GetAsyncKeyState的问题,求看看怎么回事儿
你不要简单地检测GetAsyncKeyState的返回值是否非零,因为如果在这个函数运行前指定的键已经被按过,则这个函数的返回值的位0为1;如果这个键目前处于按下状态,则位15为1。换句话说,GetAsyncKeyState这个函数可以返回键盘上某个键的两种实时状态:“曾经被按过”以及“正在被按下”!而这两种状态都会使GetAsyncKeyState的返回值非零,因此用If GetAsyncKeyState(vbKeyA) Then来判断A键是否被按下是不严密的,因为在你的程序启动之前,A键很可能已经被按过了,这种情况下你的程序一启动,GetAsyncKeyState(vbKeyA)就是为“真”的!因此你要检测返回值的位15是否为1才能够精确判断这个键是不是正在被按着:If (GetAsyncKeyState(vbKeyA) And &H8000) = &H8000 Then另外,还有一个API函数GetKeyState是只检测按键的当前状态的(也就是说它不检测这个键是否“曾经被按过”),但仍然要检测位15是否为1(位0为1表示开关键打开,即CapsLock、NumLock、ScrollLock键):If (GetKeyState(vbKeyA) And &H8000) = &H8000 Then
使用GetAsyncKeyState实现的全局热键怎么设组合键
新建模块 写入代码 Option Explicit Declare Function SetWindowLong Lib “user32“ Alias “SetWindowLongA“ (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long Declare Function GetWindowLong Lib “user32“ Alias “GetWindowLongA“ (ByVal hwnd As Long, ByVal nIndex As Long) As Long Declare Function CallWindowProc Lib “user32“ Alias “CallWindowProcA“ (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Declare Function RegisterHotKey Lib “user32“ (ByVal hwnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long Declare Function UnregisterHotKey Lib “user32“ (ByVal hwnd As Long, ByVal id As Long) As Long Public Const WM_HOTKEY = &H312 Public Const MOD_ALT = &H1 Public Const MOD_CONTROL = &H2 Public Const MOD_SHIFT = &H4 Public Const GWL_WNDPROC = (-4) Public preWinProc As Long Public Modifiers As Long, uVirtKey1 As Long, idHotKey As Long Private Type taLong ll As Long End Type Private Type t2Int lWord As Integer hword As Integer End Type Public Function wndproc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Dim lp As taLong, i2 As t2Int If Msg = WM_HOTKEY Then If wParam = idHotKey Then lp.ll = lParam LSet i2 = lp If (i2.lWord = Modifiers) And i2.hword = uVirtKey1 Then ’------------------------------------------------------ ’这里面是快捷键代码,你可以随便改 要做的事情 ’------------------------------------------------------ End If End If End If ’如果不是热键信息则调用原来的程序 wndproc = CallWindowProc(preWinProc, hwnd, Msg, wParam, lParam) End Function Form_Load中加入 Dim ret As Long ’记录原来的window程序地址 preWinProc = GetWindowLong(Me.hwnd, GWL_WNDPROC) ’用自定义程序代替原来的window程序 ret = SetWindowLong(Me.hwnd, GWL_WNDPROC, AddressOf wndproc) idHotKey = 1 ’in the range &h0000 through &hBFFF Modifiers = MOD_CONTROL ’辅助键为CTRL uVirtKey1 = 49 ’ ’注册热键 ret = RegisterHotKey(Me.hwnd, idHotKey, Modifiers, uVirtKey1) If ret = 0 Then MsgBox “注册热键失败,请使用其它热键!“, vbCritical, “错误“ End If键盘对应值’小键盘部分 96 小键盘0 97 小键盘1 98 小键盘2 99 小键盘3 100 小键盘4 101 小键盘5 102 小键盘6 103 小键盘7 104 小键盘8 105 小键盘9 106 小键盘* 107 小键盘+ 109 小键盘- 110 小键盘. 111 小键盘/13 小键盘Enter,同大键盘 144 小键盘numlock’字母 65 || A || 66 || B || 67 || C || 68 || D || 69 || E || 70 || F || 71 || G || 72 || H || 73 || I || 74 || J || 75 || K || 76 || L || 77 || M || 78 || N || 79 || O || 80 || P || 81 || Q || 82 || R || 83 || S || 84 || T || 85 || U || 86 || V || 87 || W || 88 || X || 89 || Y || 90 || Z ||’上排数字键 48 || 0 || 49 || 1 || 50 || 2 || 51 || 3 || 52 || 4 || 53 || 5 || 54 || 6 || 55 || 7 || 56 || 8 || 57 || 9 ||192 || ` || 189 || - || 187 || = || 220 || \ ||8 || 退格 ||’九键区 44 || 9键区-印屏幕键 || 45 || 9键区-插入键 || 46 || 9键区-删除键 || 145 || 9键区-滚动锁定键 || 36 || 9键区-起始键 || 35 || 9键区-结束键 || 19 || 9键区-暂停中断键 || 33 || 9键区-上页 || 34 || 9键区-下页 ||’4区 38 || 上 || 40 || 下 || 37 || 左 || 39 || 右 ||’最上区 27 || Esc || 112 || F1 || 113 || F2 || 114 || F3 || 115 || F4 || 116 || F5 || 117 || F6 || 118 || F7 || 119 || F8 || 120 || F9 || 121 || F10 || 122 || F11 || 123 || F12 ||’大键盘周区 9 || 大键盘左,TAB 20 || 大键盘左,大写锁定 160 || 大键盘左Shift 162 || 大键盘左,Ctrl 91 || 大键盘左,Win13 || 大键盘右,回车161 || 大键盘右,Shift 92 || 大键盘右,Win 93 || 大键盘右,弹菜单键 163 || 大键盘右,Ctrlvbkeylbutton 1 鼠标左键 vbkeyrbutton 2 鼠标右键 vbkeycancel 3 cancel 键 vbkeymbutton 4 鼠标中键 vbkeyback 8 backspace 键 vbkeytab 9 tab 键 vbkeyclear 12 clear 键 vbkeyreturn 13 enter 键 vbkeyshift 16 shift 键 vbkeycontrol 17 ctrl 键 vbkeymenu 18 菜单键 vbkeypause 19 pause 键 vbkeycapital 20 caps lock 键 vbkeyescape 27 esc 键 vbkeyspace 32 spacebar 键 vbkeypageup 33 pageup 键 vbkeypagedown 34 pagedown 键 vbkeyend 35 end 键 vbkeyhome 36 home 键 vbkeyleft 37 left arrow 键 vbkeyup 38 up arrow 键 vbkeyright 39 right arrow 键 vbkeydown 40 down arrow 键 vbkeyselect 41 select 键 vbkeyprint 42 print screen 键 vbkeyexecute 43 execute 键 vbkeysnapshot 44 snap shot 键 vbkeyinser 45 ins 键 vbkeydelete 46 del 键 vbkeyhelp 47 help 键 vbkeynumlock 144 num lock 键a 键到 z 键与其 ascii 码的相应值’a’ 到 ’z’ 是一致的 常数 值 描述 vbkeya 65 a 键 vbkeyb 66 b 键 vbkeyc 67 c 键 vbkeyd 68 d 键 vbkeye 69 e 键 vbkeyf 70 f 键 vbkeyg 71 g 键 vbkeyh 72 h 键 vbkeyi 73 i 键 vbkeyj 74 j 键 vbkeyk 75 k 键 vbkeyl 76 l 键 vbkeym 77 m 键 vbkeyn 78 n 键 vbkeyo 79 o 键 vbkeyp 80 p 键 vbkeyq 81 q 键 vbkeyr 82 r 键 vbkeys 83 s 键 vbkeyt 84 t 键 vbkeyu 85 u 键 vbkeyv 86 v 键 vbkeyw 87 w 键 vbkeyx 88 x 键 vbkeyy 89 y 键 vbkeyz 90 z 键0 键到 9 键与其 ascii 码的相应值 ’0’ 到 ’9’ 是一致的 常数 值 描述 vbkey0 48 0 键 vbkey1 49 1 键 vbkey2 50 2 键 vbkey3 51 3 键 vbkey4 52 4 键 vbkey5 53 5 键 vbkey6 54 6 键 vbkey7 55 7 键 vbkey8 56 8 键 vbkey9 57 9 键数字小键盘上的键 常数 值 描述 vbkeynumpad0 96 0 键 vbkeynumpad1 97 1 键 vbkeynumpad2 98 2 键 vbkeynumpad3 99 3 键 vbkeynumpad4 100 4 键 vbkeynumpad5 101 5 键 vbkeynumpad6 102 6 键 vbkeynumpad7 103 7 键 vbkeynumpad8 104 8 键 vbkeynumpad9 105 9 键 vbkeymultiply 106 乘号 (*) 键 vbkeyadd 107 加号 (+) 键 vbkeyseparator 108 enter 键(在数字小键盘上) vbkeysubtract 109 减号 (-) 键 vbkeydecimal 110 小数点 (.) 键 vbkeydivide 111 除号 (/) 键功能键 常数 值 描述 vbkeyf1 112 f1 键 vbkeyf2 113 f2 键 vbkeyf3 114 f3 键 vbkeyf4 115 f4 键 vbkeyf5 116 f5 键 vbkeyf6 117 f6 键 vbkeyf7 118 f7 键 vbkeyf8 119 f8 键 vbkeyf9 120 f9 键 vbkeyf10 121 f10 键 vbkeyf11 122 f11 键 vbkeyf12 123 f12 键 vbkeyf13 124 f13 键 vbkeyf14 125 f14 键 vbkeyf15 126 f15 键 vbkeyf16 127 f16 键KeyCode常数用法 可在代码中的任何地方用下列常数代替实际值:常数 值 描述vbKeyLButton 0x1 鼠标左键 vbKeyRButton 0x2 鼠标右键 vbKeyCancel 0x3 CANCEL 键 vbKeyMButton 0x4 鼠标中键 vbKeyBack 0x8 BACKSPACE 键 vbKeyTab 0x9 TAB 键 vbKeyClear 0xC CLEAR 键 vbKeyReturn 0xD ENTER 键 vbKeyShift 0x10 SHIFT 键 vbKeyControl 0x11 CTRL 键 vbKeyMenu 0x12 MENU 键 vbKeyPause 0x13 PAUSE 键 vbKeyCapital 0x14 CAPS LOCK 键 vbKeyEscape 0x1B ESC 键 vbKeySpace 0x20 SPACEBAR 键 vbKeyPageUp 0x21 PAGE UP 键 vbKeyPageDown 0x22 PAGE DOWN 键 vbKeyEnd 0x23 END 键 vbKeyHome 0x24 HOME 键 vbKeyLeft 0x25 LEFT ARROW 键 vbKeyUp 0x26 UP ARROW 键 vbKeyRight 0x27 RIGHT ARROW 键 vbKeyDown 0x28 DOWN ARROW 键 vbKeySelect 0x29 SELECT 键 vbKeyPrint 0x2A PRINT SCREEN 键 vbKeyExecute 0x2B EXECUTE 键 vbKeySnapshot 0x2C SNAPSHOT 键 vbKeyInsert 0x2D INSERT 键 vbKeyDelete 0x2E DELETE 键 vbKeyHelp 0x2F HELP 键 vbKeyNumlock 0x90 NUM LOCK 键 A 至 Z 键与 A 杴 Z 字母的 ASCII 码相同:常数 值 描述 vbKeyA 65 A 键 vbKeyB 66 B 键 vbKeyC 67 C 键 vbKeyD 68 D 键 vbKeyE 69 E 键 vbKeyF 70 F 键 vbKeyG 71 G 键 vbKeyH 72 H 键 vbKeyI 73 I 键 vbKeyJ 74 J 键 vbKeyK 75 K 键 vbKeyL 76 L 键 vbKeyM 77 M 键 vbKeyN 78 N 键 vbKeyO 79 O 键 vbKeyP 80 P 键 vbKeyQ 81 Q 键 vbKeyR 82 R 键 vbKeyS 83 S 键 vbKeyT 84 T 键 vbKeyU 85 U 键 vbKeyV 86 V 键 vbKeyW 87 W 键 vbKeyX 88 X 键 vbKeyY 89 Y 键 vbKeyZ 90 Z 键 0 至 9 键与数字 0 杴 9 的 ASCII 码相同:常数 值 描述vbKey0 48 0 键 vbKey1 49 1 键 vbKey2 50 2 键 vbKey3 51 3 键 vbKey4 52 4 键 vbKey5 53 5 键 vbKey6 54 6 键 vbKey7 55 7 键 vbKey8 56 8 键 vbKey9 57 9 键 下列常数代表数字键盘上的键:常数 值 描述 vbKeyNumpad0 0x60 0 键 vbKeyNumpad1 0x61 1 键 vbKeyNumpad2 0x62 2 键 vbKeyNumpad3 0x63 3 键 vbKeyNumpad4 0x64 4 键 vbKeyNumpad5 0x65 5 键 vbKeyNumpad6 0x66 6 键 vbKeyNumpad7 0x67 7 键 vbKeyNumpad8 0x68 8 键 vbKeyNumpad9 0x69 9 键 vbKeyMultiply 0x6A MULTIPLICATION SIGN (*) 键 vbKeyAdd 0x6B PLUS SIGN (+) 键 vbKeySeparator 0x6C ENTER 键 vbKeySubtract 0x6D MINUS SIGN (杴) 键 vbKeyDecimal 0x6E DECIMAL POINT (.) 键 vbKeyDivide 0x6F DIVISION SIGN (/) 键 下列常数代表功能键:常数 值 描述vbKeyF1 0x70 F1 键 vbKeyF2 0x71 F2 键 vbKeyF3 0x72 F3 键 vbKeyF4 0x73 F4 键 vbKeyF5 0x74 F5 键 vbKeyF6 0x75 F6 键 vbKeyF7 0x76 F7 键 vbKeyF8 0x77 F8 键 vbKeyF9 0x78 F9 键 vbKeyF10 0x79 F10 键 vbKeyF11 0x7A F11 键 vbKeyF12 0x7B F12 键 vbKeyF13 0x7C F13 键 vbKeyF14 0x7D F14 键 vbKeyF15 0x7E F15 键 vbKeyF16 0x7F F16 键
急!急!急!谁知道c++里的GetAsyncKeyState到底怎么用啊
下面是我的一点解释,“引号起来的内容注要意体会”,括号中的内容是我自己加的。例子是在测试程序具有键盘焦点的情况下进行的。原型:SHORT GetAsyncKeyState(int vKey);参数vKey是某个按键的虚拟码(很多虚拟码跟其ASCII码数值上相等,但不代表全部。)MSDN片段:The GetAsyncKeyState function determines whether a key is up or down at the time the function is called, and whether the key was pressed after a previous call to GetAsyncKeyState.译文:函数GetAsyncKeyState确定在调用它时某个按键处于弹起还是按下的,以及此按键是否在上一次调用GetAsyncKeyState之后(“又”)按下过(重复也算按下)。MSDN片段:If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState. However, you should not rely on this last behavior; for more information, see the Remarks. Windows NT/2000/XP: The return value is zero for the following cases: The current desktop is not the active desktopThe foreground thread belongs to another process and the desktop does not allow the hook or the journal record.Windows 95/98/Me: The return value is the global asynchronous key state for each virtual key. The system does not check which thread has the keyboard focus.Windows 95/98/Me: Windows 95 does not support the left- and right-distinguishing constants. If you call GetAsyncKeyState with these constants, the return value is zero. 译文:如果函数成功,则返回值指定此按键自上次上次调用GetAsyncKeyState后是否按下过,以及此按键当前是弹起的还是按下的。如果最高比特位被设置(就是被设为1),此按键就是按下的,如果最低位被设置,则此按键在上次调用GetAsyncKeyState之后(“又”)被按下过。但是你不应该依赖于之前的(按键)行为。更多的信息请看备注。Windows NT/2000/XP: 以下情况返回值为0* 当前桌面不是活动桌面(你有多个桌面的时候就会发生。)* 前台线程属于其他的进程并且桌面不允许次拦截(hook,名词时具有钩或类似物品的含义,动词使具有使用这些物品时的动作的含义,我采用一个根式和理解的词汇,但还不是很准)或日志记录。Windows 95/98/Me: 返回值是每个虚拟键全局异步按键状态。系统不会校对那个线程拥有键盘焦点。Windows 95/98/Me: Windows 95 不支持左右鉴别值。如果你用这些(包含左右鉴别值的)常量去调用GetAsyncKeyState时返回值为0。举例:假设下载我把A按住,调用GetAsyncKeyState(’A’)返回0x8001在按键重复之前放掉A,调用GetAsyncKeyState(’A’)返回0x0000又把A按住,又放掉,调用GetAsyncKeyState(’A’)返回0x0001又把A按住,调用GetAsyncKeyState(’A’)返回0x8001在按键重复开始后放掉A,调用GetAsyncKeyState(’A’)返回0x0001又调用GetAsyncKeyState(’A’)返回0x0000