C# monitor keyboard and print pressed key
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的更多相关文章
- C# monitor keyboard and mouse actions based on MouseKeyHook.
1.Install-package MouseKeyHook 2. using Gma.System.MouseKeyHook; using System; namespace ConsoleApp1 ...
- 使用VTK与Python实现机械臂三维模型可视化
三维可视化系统的建立依赖于三维图形平台, 如 OpenGL.VTK.OGRE.OSG等, 传统的方法多采用OpenGL进行底层编程,即对其特有的函数进行定量操作, 需要开发人员熟悉相关函数, 从而造成 ...
- OS X: Keyboard shortcuts
Using keyboard shortcuts To use a keyboard shortcut, press a modifier key at the same time as a char ...
- [转载]-虚拟键值表-virtual key code
转载 虚拟键值表, virtual key code Virtual-Key Codes VK_LBUTTON (01)Left mouse button VK_RBUTTON (02)Right ...
- Linux使用Public Key方式远程登录
一.前言: ssh远程登录密码认证的方式有三种,password.Keyboard Interactive.Public Key 前面两种方式就是密码认证,含义都是一样大同小异.第三种是登录方式最安全 ...
- 带有key参数的函数filter,map,max,min
内置函数———filter def is_not_empty(s): return s and len(s.strip()) > 0 filter(is_not_empty, ['test', ...
- 证书脚本--生成csr,key
#!/bin/sh # this script can make certificate of each line in file you point which one! if [ $# -ne 1 ...
- python 对字典分别按照key值、value值进行排序
1.sorted函数首先介绍sorted函数,sorted(iterable,key,reverse),sorted一共有iterable,key,reverse这三个参数. 其中iterable表示 ...
- Yet Another Broken Keyboard
time limit per test2 secondsmemory limit per test256 megabytesinput: standard inputoutput: standard ...
随机推荐
- 深入了解Zookeeper
ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,它包含一个简单的原语集,分布式应用程序可以基于它实现同步服务,配置维护和命名服务等.Zookeeper是hadoop的一个子项目,其 ...
- vue2.x中子组件修改父组件通过pops传递过来的值
首先,父组件向子组件传值 这里面主要是在传值的时候,加上.sync 然后子组件通过 $emit 修改 如此即可完成对父组件的数据操作
- 如何利用开源解码项目开发js视频解码的web应用 系列
介绍web上开发视频业务相关程序的技术演变历程 https://www.cnblogs.com/maoliangwu/articles/12046495.html 介绍ffmpeg asm.js we ...
- Codeforces_805
A.当l == r时,肯定输出这个数就可以了,当存在两个或两个以上连续的数时,2肯定是最多的数,或最多的数之一. #include<bits/stdc++.h> using namespa ...
- BZOJ 2653 middle (可持久化线段树+中位数+线段树维护最大子序和)
题意: 左端点在[a,b],右端点在[c,d],求这个线段里中位数(上取整)最大值 思路: 对数组离散化,对每一个值建中位数的可持久化线段树(有重复也没事),就是对于root[i],大于等于i的值为1 ...
- java4选择结构 二
public class jh_01_为什么使用switch选择结构 { /* * 韩嫣参加计算机编程大赛 * 如果获得第一名,将参加麻省理工大学组织的1个月夏令营 * 如果获得第二名,将奖励惠普笔记 ...
- Guava入门使用教程
Guava入门使用教程 Guava Maven dependency In our examples, we use the following Maven dependency. <depen ...
- mapreduce清洗数据
继上篇 MapReduce清洗数据 package mapreduce; import java.io.IOException; import org.apache.hadoop.conf.Confi ...
- Yandex Big Data Essentials Week1 Unix Command Line Interface Processes managing
free displays the total amount of free and used memory free [options] top provides a dynamic real-ti ...
- 查看php相关信息
1.最常见的就是 创建一个 php页面 ,例如 test.php, 内容如下 <?php phpinfo();?> 直接访问 这个页面,就可以看到php的 信息了 2.其它方法 直 ...