C# monitor keyboard and print pressed key
程序员文章站
2023-09-28 18:18:48
using System; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Diagnostics; namespace ConsoleApplication3 { class Monito ......
using system; using system.windows.forms; using system.runtime.interopservices; using system.diagnostics; namespace consoleapplication3 { class monitorhelper { internal const int whkeyboardllvalue = 13; 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 >= 0 && 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), 0); } } } }
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
#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 = -65536, // // summary: // no key pressed. none = 0, // // summary: // the left mouse button. lbutton = 1, // // summary: // the right mouse button. rbutton = 2, // // summary: // the cancel key. cancel = 3, // // summary: // the middle mouse button (three-button mouse). mbutton = 4, // // summary: // the first x mouse button (five-button mouse). xbutton1 = 5, // // summary: // the second x mouse button (five-button mouse). xbutton2 = 6, // // summary: // the backspace key. back = 8, // // summary: // the tab key. tab = 9, // // summary: // the linefeed key. linefeed = 10, // // summary: // the clear key. clear = 12, // // summary: // the return key. return = 13, // // summary: // the enter key. enter = 13, // // summary: // the shift key. shiftkey = 16, // // summary: // the ctrl key. controlkey = 17, // // summary: // the alt key. menu = 18, // // summary: // the pause key. pause = 19, // // summary: // the caps lock key. capital = 20, // // summary: // the caps lock key. capslock = 20, // // summary: // the ime kana mode key. kanamode = 21, // // summary: // the ime hanguel mode key. (maintained for compatibility; use hangulmode) hanguelmode = 21, // // summary: // the ime hangul mode key. hangulmode = 21, // // summary: // the ime junja mode key. junjamode = 23, // // summary: // the ime final mode key. finalmode = 24, // // summary: // the ime hanja mode key. hanjamode = 25, // // summary: // the ime kanji mode key. kanjimode = 25, // // summary: // the esc key. escape = 27, // // summary: // the ime convert key. imeconvert = 28, // // summary: // the ime nonconvert key. imenonconvert = 29, // // summary: // the ime accept key, replaces system.windows.forms.keys.imeaceept. imeaccept = 30, // // summary: // the ime accept key. obsolete, use system.windows.forms.keys.imeaccept instead. imeaceept = 30, // // summary: // the ime mode change key. imemodechange = 31, // // summary: // the spacebar key. space = 32, // // summary: // the page up key. prior = 33, // // summary: // the page up key. pageup = 33, // // summary: // the page down key. next = 34, // // summary: // the page down key. pagedown = 34, // // summary: // the end key. end = 35, // // summary: // the home key. home = 36, // // summary: // the left arrow key. left = 37, // // summary: // the up arrow key. up = 38, // // summary: // the right arrow key. right = 39, // // summary: // the down arrow key. down = 40, // // summary: // the select key. select = 41, // // summary: // the print key. print = 42, // // summary: // the execute key. execute = 43, // // summary: // the print screen key. snapshot = 44, // // summary: // the print screen key. printscreen = 44, // // summary: // the ins key. insert = 45, // // summary: // the del key. delete = 46, // // summary: // the help key. help = 47, // // summary: // the 0 key. d0 = 48, // // summary: // the 1 key. d1 = 49, // // summary: // the 2 key. d2 = 50, // // summary: // the 3 key. d3 = 51, // // summary: // the 4 key. d4 = 52, // // summary: // the 5 key. d5 = 53, // // summary: // the 6 key. d6 = 54, // // summary: // the 7 key. d7 = 55, // // summary: // the 8 key. d8 = 56, // // summary: // the 9 key. d9 = 57, // // summary: // the a key. a = 65, // // summary: // the b key. b = 66, // // summary: // the c key. c = 67, // // summary: // the d key. d = 68, // // summary: // the e key. e = 69, // // summary: // the f key. f = 70, // // summary: // the g key. g = 71, // // summary: // the h key. h = 72, // // summary: // the i key. i = 73, // // summary: // the j key. j = 74, // // summary: // the k key. k = 75, // // summary: // the l key. l = 76, // // summary: // the m key. m = 77, // // summary: // the n key. n = 78, // // summary: // the o key. o = 79, // // summary: // the p key. p = 80, // // summary: // the q key. q = 81, // // summary: // the r key. r = 82, // // summary: // the s key. s = 83, // // summary: // the t key. t = 84, // // summary: // the u key. u = 85, // // summary: // the v key. v = 86, // // summary: // the w key. w = 87, // // summary: // the x key. x = 88, // // summary: // the y key. y = 89, // // summary: // the z key. z = 90, // // summary: // the left windows logo key (microsoft natural keyboard). lwin = 91, // // summary: // the right windows logo key (microsoft natural keyboard). rwin = 92, // // summary: // the application key (microsoft natural keyboard). apps = 93, // // summary: // the computer sleep key. sleep = 95, // // summary: // the 0 key on the numeric keypad. numpad0 = 96, // // summary: // the 1 key on the numeric keypad. numpad1 = 97, // // summary: // the 2 key on the numeric keypad. numpad2 = 98, // // summary: // the 3 key on the numeric keypad. numpad3 = 99, // // summary: // the 4 key on the numeric keypad. numpad4 = 100, // // summary: // the 5 key on the numeric keypad. numpad5 = 101, // // summary: // the 6 key on the numeric keypad. numpad6 = 102, // // summary: // the 7 key on the numeric keypad. numpad7 = 103, // // summary: // the 8 key on the numeric keypad. numpad8 = 104, // // summary: // the 9 key on the numeric keypad. numpad9 = 105, // // summary: // the multiply key. multiply = 106, // // summary: // the add key. add = 107, // // summary: // the separator key. separator = 108, // // summary: // the subtract key. subtract = 109, // // summary: // the decimal key. decimal = 110, // // summary: // the divide key. divide = 111, // // summary: // the f1 key. f1 = 112, // // summary: // the f2 key. f2 = 113, // // summary: // the f3 key. f3 = 114, // // summary: // the f4 key. f4 = 115, // // summary: // the f5 key. f5 = 116, // // summary: // the f6 key. f6 = 117, // // summary: // the f7 key. f7 = 118, // // summary: // the f8 key. f8 = 119, // // summary: // the f9 key. f9 = 120, // // summary: // the f10 key. f10 = 121, // // summary: // the f11 key. f11 = 122, // // summary: // the f12 key. f12 = 123, // // summary: // the f13 key. f13 = 124, // // summary: // the f14 key. f14 = 125, // // summary: // the f15 key. f15 = 126, // // summary: // the f16 key. f16 = 127, // // summary: // the f17 key. f17 = 128, // // summary: // the f18 key. f18 = 129, // // summary: // the f19 key. f19 = 130, // // summary: // the f20 key. f20 = 131, // // summary: // the f21 key. f21 = 132, // // summary: // the f22 key. f22 = 133, // // summary: // the f23 key. f23 = 134, // // summary: // the f24 key. f24 = 135, // // summary: // the num lock key. numlock = 144, // // summary: // the scroll lock key. scroll = 145, // // summary: // the left shift key. lshiftkey = 160, // // summary: // the right shift key. rshiftkey = 161, // // summary: // the left ctrl key. lcontrolkey = 162, // // summary: // the right ctrl key. rcontrolkey = 163, // // summary: // the left alt key. lmenu = 164, // // summary: // the right alt key. rmenu = 165, // // summary: // the browser back key (windows 2000 or later). browserback = 166, // // summary: // the browser forward key (windows 2000 or later). browserforward = 167, // // summary: // the browser refresh key (windows 2000 or later). browserrefresh = 168, // // summary: // the browser stop key (windows 2000 or later). browserstop = 169, // // summary: // the browser search key (windows 2000 or later). browsersearch = 170, // // summary: // the browser favorites key (windows 2000 or later). browserfavorites = 171, // // summary: // the browser home key (windows 2000 or later). browserhome = 172, // // summary: // the volume mute key (windows 2000 or later). volumemute = 173, // // summary: // the volume down key (windows 2000 or later). volumedown = 174, // // summary: // the volume up key (windows 2000 or later). volumeup = 175, // // summary: // the media next track key (windows 2000 or later). medianexttrack = 176, // // summary: // the media previous track key (windows 2000 or later). mediaprevioustrack = 177, // // summary: // the media stop key (windows 2000 or later). mediastop = 178, // // summary: // the media play pause key (windows 2000 or later). mediaplaypause = 179, // // summary: // the launch mail key (windows 2000 or later). launchmail = 180, // // summary: // the select media key (windows 2000 or later). selectmedia = 181, // // summary: // the start application one key (windows 2000 or later). launchapplication1 = 182, // // summary: // the start application two key (windows 2000 or later). launchapplication2 = 183, // // summary: // the oem semicolon key on a us standard keyboard (windows 2000 or later). oemsemicolon = 186, // // summary: // the oem 1 key. oem1 = 186, // // summary: // the oem plus key on any country/region keyboard (windows 2000 or later). oemplus = 187, // // summary: // the oem comma key on any country/region keyboard (windows 2000 or later). oemcomma = 188, // // summary: // the oem minus key on any country/region keyboard (windows 2000 or later). oemminus = 189, // // summary: // the oem period key on any country/region keyboard (windows 2000 or later). oemperiod = 190, // // summary: // the oem question mark key on a us standard keyboard (windows 2000 or later). oemquestion = 191, // // summary: // the oem 2 key. oem2 = 191, // // summary: // the oem tilde key on a us standard keyboard (windows 2000 or later). oemtilde = 192, // // summary: // the oem 3 key. oem3 = 192, // // summary: // the oem open bracket key on a us standard keyboard (windows 2000 or later). oemopenbrackets = 219, // // summary: // the oem 4 key. oem4 = 219, // // summary: // the oem pipe key on a us standard keyboard (windows 2000 or later). oempipe = 220, // // summary: // the oem 5 key. oem5 = 220, // // summary: // the oem close bracket key on a us standard keyboard (windows 2000 or later). oemclosebrackets = 221, // // summary: // the oem 6 key. oem6 = 221, // // summary: // the oem singled/double quote key on a us standard keyboard (windows 2000 or later). oemquotes = 222, // // summary: // the oem 7 key. oem7 = 222, // // summary: // the oem 8 key. oem8 = 223, // // summary: // the oem angle bracket or backslash key on the rt 102 key keyboard (windows 2000 // or later). oembackslash = 226, // // summary: // the oem 102 key. oem102 = 226, // // summary: // the process key key. processkey = 229, // // 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 = 231, // // summary: // the attn key. attn = 246, // // summary: // the crsel key. crsel = 247, // // summary: // the exsel key. exsel = 248, // // summary: // the erase eof key. eraseeof = 249, // // summary: // the play key. play = 250, // // summary: // the zoom key. zoom = 251, // // summary: // a constant reserved for future use. noname = 252, // // summary: // the pa1 key. pa1 = 253, // // summary: // the clear key. oemclear = 254, // // summary: // the bitmask to extract a key code from a key value. keycode = 65535, // // summary: // the shift modifier key. shift = 65536, // // summary: // the ctrl modifier key. control = 131072, // // summary: // the alt modifier key. alt = 262144 } }
上一篇: c# 读写SerialPort
下一篇: form中把图片设为按钮