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. Hyper-V虚拟机设置外部网络访问

    在Hyper-V管理器中新建一个虚拟交换机,类型为 内部 ,修改名称为 nat 在虚拟机的设置页面中,将网络适配器设置为新建的虚拟交换机 nat 打开win10->控制面板->网络和共享中 ...

  2. JS刷算法题:二叉树

    Q1.翻转二叉树(easy) 如题所示 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 来源:力扣(LeetCode) ...

  3. Codeforces gym101755H Safe Path(bfs)

    题意: 给以一个网格图,有起点终点和一些怪兽,可以上下左右走,不能走到距离怪兽曼哈顿距离为d以内的地方,问到终点最短路径 n*m<=2e5,d<=2e5 思路: 因为n*m的范围,不能直接 ...

  4. 研究微信红包分配算法之Golang版

    今天来看一下红包的分配,参考几年前流传的微信红包分配算法,今天用Golang实现一版,并测试验证结果. 微信红包的随机算法是怎样实现的?https://www.zhihu.com/question/2 ...

  5. Go语言实现:【剑指offer】数字在排序数组中出现的次数

    该题目来源于牛客网<剑指offer>专题. 统计一个数字在排序数组中出现的次数. 看到排序数组,要想到用二分查找. 先找到最前面的数字k,再找到最后面的数字k,通过下标求出次数. Go语言 ...

  6. Java线程核心基础(上)

    Java线程核心基础(上) 一.实现多线程 根据Oracle官方文档,目前推荐的创建线程方法主要有两种,分别是继承Thread类和实现Runnable接口.通过阅读Thread类源码,可以发现二者不同 ...

  7. CP - OFDM

  8. 06-HTML

    今日知识 1. HTML基本语法 2. 特殊符号表示 3.总结 HTML 1. Hyper Text Mark Language 超文本标记语言 * 超文本: * 超文本是用超链接的方法,将各种不同空 ...

  9. 吴恩达deepLearning.ai循环神经网络RNN学习笔记_没有复杂数学公式,看图就懂了!!!(理论篇)

    本篇文章被Google中国社区组织人转发,评价: 条理清晰,写的很详细! 被阿里算法工程师点在看! 所以很值得一看! 前言 目录: RNN提出的背景 - 一个问题 - 为什么不用标准神经网络 - RN ...

  10. 1163 - Bank Robbery

    1163 - Bank Robbery   In one very cold morning, Mark decides to rob a bank. But while trying hacking ...