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. Percona-XtraDB-Cluster-57 安装操作记录

    一.PXC集群的一些特性 Percona官网服务器位于境外,访问很困难.本次安装使用的是其官网提供的最新版本5.7.23-31.31.1.el7,当前日期为2018.10.10. PXC集群中,存储引 ...

  2. HTTPS 详解一:附带最精美详尽的 HTTPS 原理图

    HTTPS 详解一:附带最精美详尽的 HTTPS 原理图 HTTPS详解二:SSL / TLS 工作原理和详细握手过程 前言 作为一个有追求的程序员,了解行业发展趋势和扩充自己的计算机知识储备都是很有 ...

  3. IntelliJ IDEA 2020 的Debug功能也太好用了,真香!

    写在前边 作为一个有点强迫症的程序员来说,所有的应用软件.开发工具都必须要升级到最高版本,否则就会很难受到坐立不安.日思夜想.茶饭不思.至于什么时候得的这种病我也记不清了,哈哈哈 IntelliJ I ...

  4. oracle11g和12c区别

    11g和12c 1.12c使用更为强大的sql执行与优化算法 2.oracle在12c完全使用云和可插拔数据库概念 3.oracle 12c的RAC使用flex(让rg直接化) 模式,让rg管理更加细 ...

  5. Ceph 存储集群5-数据归置

    一.数据归置概览 Ceph 通过 RADOS 集群动态地存储.复制和重新均衡数据对象.很多不同用户因不同目的把对象存储在不同的存储池里,而它们都坐落于无数的 OSD 之上,所以 Ceph 的运营需要些 ...

  6. Android 开启与关闭软键盘

    http://www.cnblogs.com/weixing/p/3300908.html InputMethodManager imm = (InputMethodManager)getSystem ...

  7. CSS 对于grid布局的理解,举例代码及解释

    网格布局介绍: CSS Grid(网格) 布局(又称为 “Grid(网格)” ),是一个二维的基于网格的布局系统它的目标是完全改变我们基于网格的用户界面的布局方式.CSS 一直用来布局我们的网页,但一 ...

  8. [web]2019第一起数据泄露事件

    -rwxrwxrwx 33405108 Jan 22 2016 000webhost.txt -rwxrwxrwx 165025 Jul 29 2017 01nii.ru {1.931} [HASH] ...

  9. CCF_201604-1_折点计数

    (a[i]-a[i-1])*(a[i]-a[i+1]) > 0 的点符合条件 #include<cstdio> #include<iostream> using name ...

  10. [转载]goldendict下优质词典简介及安装

    使用Arch Linux一年以来,如果要问自己最为中意的词典程序是? 当然是Goldendict啦!想详细了解这款瑞士军刀的请猛戳这里. 以前在Win下都是用的lingoes, 感觉还不错,词典库很全 ...