using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics; namespace ConsoleApplication3
{
class MonitorHelper
{
internal const int WHKEYBOARDLLValue = ;
internal const int WMKEYDOWNValue = 0x0100;
internal static LowLevelKeyboardProc procValue = HookCallback;
internal static IntPtr hookIDValue = IntPtr.Zero;
internal delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool UnhookWindowsHookEx(IntPtr hhk); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam); [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern IntPtr GetModuleHandle(string lpModuleName); internal static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= && wParam == (IntPtr)WMKEYDOWNValue)
{
int vkCode = Marshal.ReadInt32(lParam);
Console.WriteLine((Keys)vkCode);
} return CallNextHookEx(hookIDValue, nCode, wParam, lParam);
} internal static IntPtr SetHook(LowLevelKeyboardProc proc)
{
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
return SetWindowsHookEx(WHKEYBOARDLLValue, proc,
GetModuleHandle(curModule.ModuleName), );
}
}
}
}
using System.Windows.Forms;
static void Main(string[] args)
{
MonitorDemo();
Console.ReadLine();
} static void MonitorDemo()
{
MonitorHelper.hookIDValue = MonitorHelper.SetHook(MonitorHelper.procValue);
Application.Run();
MonitorHelper.UnhookWindowsHookEx(MonitorHelper.hookIDValue);
}

referenced  https://stackoverflow.com/questions/604410/global-keyboard-capture-in-c-sharp-application

#region Assembly System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\System.Windows.Forms.dll
#endregion using System.ComponentModel;
using System.Runtime.InteropServices; namespace System.Windows.Forms
{
//
// Summary:
// Specifies key codes and modifiers.
[ComVisible(true)]
[Editor("System.Windows.Forms.Design.ShortcutKeysEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(Drawing.Design.UITypeEditor))]
[Flags]
[TypeConverter(typeof(KeysConverter))]
public enum Keys
{
//
// Summary:
// The bitmask to extract modifiers from a key value.
Modifiers = -,
//
// Summary:
// No key pressed.
None = ,
//
// Summary:
// The left mouse button.
LButton = ,
//
// Summary:
// The right mouse button.
RButton = ,
//
// Summary:
// The CANCEL key.
Cancel = ,
//
// Summary:
// The middle mouse button (three-button mouse).
MButton = ,
//
// Summary:
// The first x mouse button (five-button mouse).
XButton1 = ,
//
// Summary:
// The second x mouse button (five-button mouse).
XButton2 = ,
//
// Summary:
// The BACKSPACE key.
Back = ,
//
// Summary:
// The TAB key.
Tab = ,
//
// Summary:
// The LINEFEED key.
LineFeed = ,
//
// Summary:
// The CLEAR key.
Clear = ,
//
// Summary:
// The RETURN key.
Return = ,
//
// Summary:
// The ENTER key.
Enter = ,
//
// Summary:
// The SHIFT key.
ShiftKey = ,
//
// Summary:
// The CTRL key.
ControlKey = ,
//
// Summary:
// The ALT key.
Menu = ,
//
// Summary:
// The PAUSE key.
Pause = ,
//
// Summary:
// The CAPS LOCK key.
Capital = ,
//
// Summary:
// The CAPS LOCK key.
CapsLock = ,
//
// Summary:
// The IME Kana mode key.
KanaMode = ,
//
// Summary:
// The IME Hanguel mode key. (maintained for compatibility; use HangulMode)
HanguelMode = ,
//
// Summary:
// The IME Hangul mode key.
HangulMode = ,
//
// Summary:
// The IME Junja mode key.
JunjaMode = ,
//
// Summary:
// The IME final mode key.
FinalMode = ,
//
// Summary:
// The IME Hanja mode key.
HanjaMode = ,
//
// Summary:
// The IME Kanji mode key.
KanjiMode = ,
//
// Summary:
// The ESC key.
Escape = ,
//
// Summary:
// The IME convert key.
IMEConvert = ,
//
// Summary:
// The IME nonconvert key.
IMENonconvert = ,
//
// Summary:
// The IME accept key, replaces System.Windows.Forms.Keys.IMEAceept.
IMEAccept = ,
//
// Summary:
// The IME accept key. Obsolete, use System.Windows.Forms.Keys.IMEAccept instead.
IMEAceept = ,
//
// Summary:
// The IME mode change key.
IMEModeChange = ,
//
// Summary:
// The SPACEBAR key.
Space = ,
//
// Summary:
// The PAGE UP key.
Prior = ,
//
// Summary:
// The PAGE UP key.
PageUp = ,
//
// Summary:
// The PAGE DOWN key.
Next = ,
//
// Summary:
// The PAGE DOWN key.
PageDown = ,
//
// Summary:
// The END key.
End = ,
//
// Summary:
// The HOME key.
Home = ,
//
// Summary:
// The LEFT ARROW key.
Left = ,
//
// Summary:
// The UP ARROW key.
Up = ,
//
// Summary:
// The RIGHT ARROW key.
Right = ,
//
// Summary:
// The DOWN ARROW key.
Down = ,
//
// Summary:
// The SELECT key.
Select = ,
//
// Summary:
// The PRINT key.
Print = ,
//
// Summary:
// The EXECUTE key.
Execute = ,
//
// Summary:
// The PRINT SCREEN key.
Snapshot = ,
//
// Summary:
// The PRINT SCREEN key.
PrintScreen = ,
//
// Summary:
// The INS key.
Insert = ,
//
// Summary:
// The DEL key.
Delete = ,
//
// Summary:
// The HELP key.
Help = ,
//
// Summary:
// The 0 key.
D0 = ,
//
// Summary:
// The 1 key.
D1 = ,
//
// Summary:
// The 2 key.
D2 = ,
//
// Summary:
// The 3 key.
D3 = ,
//
// Summary:
// The 4 key.
D4 = ,
//
// Summary:
// The 5 key.
D5 = ,
//
// Summary:
// The 6 key.
D6 = ,
//
// Summary:
// The 7 key.
D7 = ,
//
// Summary:
// The 8 key.
D8 = ,
//
// Summary:
// The 9 key.
D9 = ,
//
// Summary:
// The A key.
A = ,
//
// Summary:
// The B key.
B = ,
//
// Summary:
// The C key.
C = ,
//
// Summary:
// The D key.
D = ,
//
// Summary:
// The E key.
E = ,
//
// Summary:
// The F key.
F = ,
//
// Summary:
// The G key.
G = ,
//
// Summary:
// The H key.
H = ,
//
// Summary:
// The I key.
I = ,
//
// Summary:
// The J key.
J = ,
//
// Summary:
// The K key.
K = ,
//
// Summary:
// The L key.
L = ,
//
// Summary:
// The M key.
M = ,
//
// Summary:
// The N key.
N = ,
//
// Summary:
// The O key.
O = ,
//
// Summary:
// The P key.
P = ,
//
// Summary:
// The Q key.
Q = ,
//
// Summary:
// The R key.
R = ,
//
// Summary:
// The S key.
S = ,
//
// Summary:
// The T key.
T = ,
//
// Summary:
// The U key.
U = ,
//
// Summary:
// The V key.
V = ,
//
// Summary:
// The W key.
W = ,
//
// Summary:
// The X key.
X = ,
//
// Summary:
// The Y key.
Y = ,
//
// Summary:
// The Z key.
Z = ,
//
// Summary:
// The left Windows logo key (Microsoft Natural Keyboard).
LWin = ,
//
// Summary:
// The right Windows logo key (Microsoft Natural Keyboard).
RWin = ,
//
// Summary:
// The application key (Microsoft Natural Keyboard).
Apps = ,
//
// Summary:
// The computer sleep key.
Sleep = ,
//
// Summary:
// The 0 key on the numeric keypad.
NumPad0 = ,
//
// Summary:
// The 1 key on the numeric keypad.
NumPad1 = ,
//
// Summary:
// The 2 key on the numeric keypad.
NumPad2 = ,
//
// Summary:
// The 3 key on the numeric keypad.
NumPad3 = ,
//
// Summary:
// The 4 key on the numeric keypad.
NumPad4 = ,
//
// Summary:
// The 5 key on the numeric keypad.
NumPad5 = ,
//
// Summary:
// The 6 key on the numeric keypad.
NumPad6 = ,
//
// Summary:
// The 7 key on the numeric keypad.
NumPad7 = ,
//
// Summary:
// The 8 key on the numeric keypad.
NumPad8 = ,
//
// Summary:
// The 9 key on the numeric keypad.
NumPad9 = ,
//
// Summary:
// The multiply key.
Multiply = ,
//
// Summary:
// The add key.
Add = ,
//
// Summary:
// The separator key.
Separator = ,
//
// Summary:
// The subtract key.
Subtract = ,
//
// Summary:
// The decimal key.
Decimal = ,
//
// Summary:
// The divide key.
Divide = ,
//
// Summary:
// The F1 key.
F1 = ,
//
// Summary:
// The F2 key.
F2 = ,
//
// Summary:
// The F3 key.
F3 = ,
//
// Summary:
// The F4 key.
F4 = ,
//
// Summary:
// The F5 key.
F5 = ,
//
// Summary:
// The F6 key.
F6 = ,
//
// Summary:
// The F7 key.
F7 = ,
//
// Summary:
// The F8 key.
F8 = ,
//
// Summary:
// The F9 key.
F9 = ,
//
// Summary:
// The F10 key.
F10 = ,
//
// Summary:
// The F11 key.
F11 = ,
//
// Summary:
// The F12 key.
F12 = ,
//
// Summary:
// The F13 key.
F13 = ,
//
// Summary:
// The F14 key.
F14 = ,
//
// Summary:
// The F15 key.
F15 = ,
//
// Summary:
// The F16 key.
F16 = ,
//
// Summary:
// The F17 key.
F17 = ,
//
// Summary:
// The F18 key.
F18 = ,
//
// Summary:
// The F19 key.
F19 = ,
//
// Summary:
// The F20 key.
F20 = ,
//
// Summary:
// The F21 key.
F21 = ,
//
// Summary:
// The F22 key.
F22 = ,
//
// Summary:
// The F23 key.
F23 = ,
//
// Summary:
// The F24 key.
F24 = ,
//
// Summary:
// The NUM LOCK key.
NumLock = ,
//
// Summary:
// The SCROLL LOCK key.
Scroll = ,
//
// Summary:
// The left SHIFT key.
LShiftKey = ,
//
// Summary:
// The right SHIFT key.
RShiftKey = ,
//
// Summary:
// The left CTRL key.
LControlKey = ,
//
// Summary:
// The right CTRL key.
RControlKey = ,
//
// Summary:
// The left ALT key.
LMenu = ,
//
// Summary:
// The right ALT key.
RMenu = ,
//
// Summary:
// The browser back key (Windows 2000 or later).
BrowserBack = ,
//
// Summary:
// The browser forward key (Windows 2000 or later).
BrowserForward = ,
//
// Summary:
// The browser refresh key (Windows 2000 or later).
BrowserRefresh = ,
//
// Summary:
// The browser stop key (Windows 2000 or later).
BrowserStop = ,
//
// Summary:
// The browser search key (Windows 2000 or later).
BrowserSearch = ,
//
// Summary:
// The browser favorites key (Windows 2000 or later).
BrowserFavorites = ,
//
// Summary:
// The browser home key (Windows 2000 or later).
BrowserHome = ,
//
// Summary:
// The volume mute key (Windows 2000 or later).
VolumeMute = ,
//
// Summary:
// The volume down key (Windows 2000 or later).
VolumeDown = ,
//
// Summary:
// The volume up key (Windows 2000 or later).
VolumeUp = ,
//
// Summary:
// The media next track key (Windows 2000 or later).
MediaNextTrack = ,
//
// Summary:
// The media previous track key (Windows 2000 or later).
MediaPreviousTrack = ,
//
// Summary:
// The media Stop key (Windows 2000 or later).
MediaStop = ,
//
// Summary:
// The media play pause key (Windows 2000 or later).
MediaPlayPause = ,
//
// Summary:
// The launch mail key (Windows 2000 or later).
LaunchMail = ,
//
// Summary:
// The select media key (Windows 2000 or later).
SelectMedia = ,
//
// Summary:
// The start application one key (Windows 2000 or later).
LaunchApplication1 = ,
//
// Summary:
// The start application two key (Windows 2000 or later).
LaunchApplication2 = ,
//
// Summary:
// The OEM Semicolon key on a US standard keyboard (Windows 2000 or later).
OemSemicolon = ,
//
// Summary:
// The OEM 1 key.
Oem1 = ,
//
// Summary:
// The OEM plus key on any country/region keyboard (Windows 2000 or later).
Oemplus = ,
//
// Summary:
// The OEM comma key on any country/region keyboard (Windows 2000 or later).
Oemcomma = ,
//
// Summary:
// The OEM minus key on any country/region keyboard (Windows 2000 or later).
OemMinus = ,
//
// Summary:
// The OEM period key on any country/region keyboard (Windows 2000 or later).
OemPeriod = ,
//
// Summary:
// The OEM question mark key on a US standard keyboard (Windows 2000 or later).
OemQuestion = ,
//
// Summary:
// The OEM 2 key.
Oem2 = ,
//
// Summary:
// The OEM tilde key on a US standard keyboard (Windows 2000 or later).
Oemtilde = ,
//
// Summary:
// The OEM 3 key.
Oem3 = ,
//
// Summary:
// The OEM open bracket key on a US standard keyboard (Windows 2000 or later).
OemOpenBrackets = ,
//
// Summary:
// The OEM 4 key.
Oem4 = ,
//
// Summary:
// The OEM pipe key on a US standard keyboard (Windows 2000 or later).
OemPipe = ,
//
// Summary:
// The OEM 5 key.
Oem5 = ,
//
// Summary:
// The OEM close bracket key on a US standard keyboard (Windows 2000 or later).
OemCloseBrackets = ,
//
// Summary:
// The OEM 6 key.
Oem6 = ,
//
// Summary:
// The OEM singled/double quote key on a US standard keyboard (Windows 2000 or later).
OemQuotes = ,
//
// Summary:
// The OEM 7 key.
Oem7 = ,
//
// Summary:
// The OEM 8 key.
Oem8 = ,
//
// Summary:
// The OEM angle bracket or backslash key on the RT 102 key keyboard (Windows 2000
// or later).
OemBackslash = ,
//
// Summary:
// The OEM 102 key.
Oem102 = ,
//
// Summary:
// The PROCESS KEY key.
ProcessKey = ,
//
// Summary:
// Used to pass Unicode characters as if they were keystrokes. The Packet key value
// is the low word of a 32-bit virtual-key value used for non-keyboard input methods.
Packet = ,
//
// Summary:
// The ATTN key.
Attn = ,
//
// Summary:
// The CRSEL key.
Crsel = ,
//
// Summary:
// The EXSEL key.
Exsel = ,
//
// Summary:
// The ERASE EOF key.
EraseEof = ,
//
// Summary:
// The PLAY key.
Play = ,
//
// Summary:
// The ZOOM key.
Zoom = ,
//
// Summary:
// A constant reserved for future use.
NoName = ,
//
// Summary:
// The PA1 key.
Pa1 = ,
//
// Summary:
// The CLEAR key.
OemClear = ,
//
// Summary:
// The bitmask to extract a key code from a key value.
KeyCode = ,
//
// Summary:
// The SHIFT modifier key.
Shift = ,
//
// Summary:
// The CTRL modifier key.
Control = ,
//
// Summary:
// The ALT modifier key.
Alt =
}
}

C# monitor keyboard and print pressed key的更多相关文章

  1. C# monitor keyboard and mouse actions based on MouseKeyHook.

    1.Install-package MouseKeyHook 2. using Gma.System.MouseKeyHook; using System; namespace ConsoleApp1 ...

  2. 使用VTK与Python实现机械臂三维模型可视化

    三维可视化系统的建立依赖于三维图形平台, 如 OpenGL.VTK.OGRE.OSG等, 传统的方法多采用OpenGL进行底层编程,即对其特有的函数进行定量操作, 需要开发人员熟悉相关函数, 从而造成 ...

  3. OS X: Keyboard shortcuts

    Using keyboard shortcuts To use a keyboard shortcut, press a modifier key at the same time as a char ...

  4. [转载]-虚拟键值表-virtual key code

    转载  虚拟键值表, virtual key code Virtual-Key Codes VK_LBUTTON (01)Left mouse button VK_RBUTTON (02)Right ...

  5. Linux使用Public Key方式远程登录

    一.前言: ssh远程登录密码认证的方式有三种,password.Keyboard Interactive.Public Key 前面两种方式就是密码认证,含义都是一样大同小异.第三种是登录方式最安全 ...

  6. 带有key参数的函数filter,map,max,min

    内置函数———filter def is_not_empty(s): return s and len(s.strip()) > 0 filter(is_not_empty, ['test', ...

  7. 证书脚本--生成csr,key

    #!/bin/sh # this script can make certificate of each line in file you point which one! if [ $# -ne 1 ...

  8. python 对字典分别按照key值、value值进行排序

    1.sorted函数首先介绍sorted函数,sorted(iterable,key,reverse),sorted一共有iterable,key,reverse这三个参数. 其中iterable表示 ...

  9. Yet Another Broken Keyboard

    time limit per test2 secondsmemory limit per test256 megabytesinput: standard inputoutput: standard ...

随机推荐

  1. Serverless 的运行原理与组件架构

    本文重点探讨下开发者使用 Serverless 时经常遇到的一些问题,以及如何解决 过去一年,我们和大量 Serverless 用户进行了线上和线下的交流,了解大家的业务场景.对 Serverless ...

  2. Unreal Engine 4 蓝图完全学习教程(一)—— 简要介绍

    首先启动UE4: 新建项目类型为游戏: 选择空项目Blank: 项目设置选项: 点击创建项目: 打开后的窗口称为:“关卡编辑器”,由多个面板组成.在UE中,设计3D场景的空间称为“关卡”. 简单介绍一 ...

  3. Solaris磁盘镜像恢复

    注:此文章笔者实验记录,欢迎大家指正 Solaris磁盘镜像恢复方法一: 系统启动,开机提示子镜像需要维护: 查看磁盘镜像信息 进入系统后,metastat -pc 和metadb #查看镜像状态与m ...

  4. 应用层vc实现三种文件监视方法

    http://hi.baidu.com/sadusaga/item/daa0d4b764c6dd76254b09cc http://bbs.csdn.net/topics/280032788 http ...

  5. Android头像更换之详细操作

    Android开发之头像的更换(拍照,从手机照片中选择) 先说一下昨天未解决的问题:原因是自己在获取对象时,没有将新加的图片属性加到该对象里,导致一直爆空指针异常. 接下来分析一下头像更换的具体操作: ...

  6. PAT_B数素数 (20)

    数素数 (20) 时间限制 1000 ms 内存限制 32768 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小) 题目描述 令Pi表示第i个素数.现任给两个正整数M &l ...

  7. Bayesian Non-Exhaustive Classification A case study:online name disambiguation using temporal record streams

    一 摘要: name entity disambiguation:将对应多个人的记录进行分组,使得每个组的记录对应一个人. 现有的方法多为批处理方式,需要将所有的记录输入给算法. 现实环境需要1:以o ...

  8. POJ_2593_DP

    http://poj.org/problem?id=2593 和2479一样. #include<iostream> #include<cstdio> #define MIN ...

  9. HDU_1864_01背包

    http://acm.hdu.edu.cn/showproblem.php?pid=1864 题目好像是输入的数据都是两位小数,先统计能报销的发票,然后把小数*100变成成熟就是01背包问题了. #i ...

  10. USBWebServer - 在U盘里搭一个Web服务器!

    文章选自我的博客:https://blog.ljyngup.com/archives/321.html/ 本文将介绍一款可以在U盘内直接搭建Web服务器的软件 软件可以免安装直接在U盘内运行,适合外出 ...