获取键盘扫描码

「已注销」 2014-07-10 10:05:47
如何用C/C++在VS2010中获取键盘扫描码?
...全文
668 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2014-07-11
  • 打赏
  • 举报
回复
好,顶一个,多谢分享
阿先森 2014-07-11
  • 打赏
  • 举报
回复
楼上发这么长的注释干嘛,一看就是CTRL+C,CTRL+V的操作。 获取键盘输入系列函数: SHORT WINAPI GetKeyState( _In_ int nVirtKey ); BOOL WINAPI GetKeyboardState( _Out_ PBYTE lpKeyState );
赵4老师 2014-07-11
  • 打赏
  • 举报
回复
Virtual-Key Codes Introduction The virtual-key codes provide a hardware- and language-independent method of identifying keyboard keys. Each virtual-key code represents a unique keyboard key and also identifies the purpose of that key. The keyboard driver must provide one or more keyboard layouts that maps keyboard scan codes to the appropriate virtual-key codes. Required Virtual Keys The complete virtual-key code set consists of 256 byte values in the range 0 to 255. Most, but not all, of the values used by standard drivers are in the range 0 to 127. There is a loose distinction between "standard" keys, which do not vary much, and "OEM" keys, which do vary from keyboard to keyboard. For full Windows functionality, a keyboard driver must provide the following virtual keys: Virtual-key code Description VK_0 through VK_9 Number keys (not on numeric keypad): 0 through 9 VK_A through VK_Z Letter keys: A through Z VK_BACK BACKSPACE key VK_CONTROL CTRL key VK_DELETE DELETE key VK_DOWN Down direction key VK_END END key VK_ESCAPE ESC key VK_F1 through VK_F10 F1 through F10 keys VK_HOME HOME key VK_INSERT INSERT key VK_LEFT Left direction key VK_MENU ALT key VK_NEXT PAGE DOWN key VK_PRIOR PAGE UP key VK_RIGHT Right direction key VK_SHIFT Left and right SHIFT keys VK_SNAPSHOT PRINTSCREEN key VK_SPACE SPACE key VK_TAB TAB key VK_UP Up direction key Shift keys must be available in the combinations unshifted, SHIFT, CTRL, and CTRL+ALT. Most keyboards, the virtual-key codes VK_INSERT, VK_DELETE, VK_HOME, VK_END, VK_PRIOR, and VK_NEXT are generated on the numeric keypad only if NUMLOCK is off. Optional Virtual Keys If a keyboard has a numeric pad, the numeric keys are frequently used as cursor-control and editing keys if NUMLOCK is off. If NUMLOCK is on, the virtual-key codes VK_NUMPAD0 through VK_NUMPAD9 are used for the digits. Keyboards with a DELETE key that also generates the decimal point (period or comma) use VK_DELETE and VK_DECIMAL to distinguish between the two uses of the key. Keyboards commonly contain various "lock" keys, such as VK_CAPITAL and VK_NUMLOCK. If a keyboard driver generates ANSI characters on the numeric keypad using ALT + numeric-pad keys, it must do this translation only if NUMLOCK is on. The cursor and editing keys on IBM-compatible keyboards do not produce this translation. Other keys may vary from keyboard to keyboard. The following set of virtual-key codes is generally used for punctuation keys, accented letter keys, and dead keys in the main section of a keyboard: VK_OEM_1 ... VK_OEM_8 VK_OEM_102 VK_OEM_PLUS, VK_OEM_MINUS, VK_OEM_COMMA, VK_OEM_PERIOD If a keyboard has more than 16 function keys, the virtual-key codes in the range VK_F17 through VK_F24 should be used for the extra function keys. Windows internally generates the mouse-button virtual-key codes VK_LBUTTON, VK_RBUTTON, VK_MBUTTON, and VK_CANCEL. These virtual-key codes are generated internally by Windows and are never generated by keyboard or mouse drivers. Keyboard drivers should not generate VK_EXECUTE or VK_SEPARATER. Virtual Keys for 122-Key Keyboards For keyboards with 122 keys, the mapping for the first 101 keys is the same as described in previous sections. The function keys F13 through F24 should use VK_F13 through VK_F24. The remaining keys should be mapped to the following virtual-key codes: VK_ATTN VK_OEM_CLEAR VK_CRSEL VK_PA1 VK_EREOF VK_PLAY VK_EXSEL VK_ZOOM VK_NONAME Virtual-Key Code Definitions The following table includes the virtual-key codes that are defined for Windows. The key code values 0 and 0FFh are not used. Virtual-key Codes Name Value Comment VK_LBUTTON 01h Left mouse button. VK_RBUTTON 02h Right mouse button. VK_CANCEL 03h Used for control-break processing. VK_MBUTTON 04h Middle mouse button (3-button mouse). 05h through 07h undefined. VK_BACK 08h VK_TAB 09h 0Ah and 0Bh undefined. VK_CLEAR 0Ch VK_RETURN 0Dh 0Eh and 0Fh undefined. VK_SHIFT 10h VK_CONTROL 11h VK_MENU 12h VK_PAUSE 13h VK_CAPITAL 14h 15h through 1Ah undefined. VK_ESCAPE 1Bh 1Ch through 1Fh undefined. VK_SPACE 20h VK_PRIOR 21h Page up. VK_NEXT 22h Page down. VK_END 23h VK_HOME 24h VK_LEFT 25h VK_UP 26h VK_RIGHT 27h VK_DOWN 28h VK_SELECT 29h VK_PRINT 2Ah Only used by Nokia. VK_EXECUTE 2Bh Never used. VK_SNAPSHOT 2Ch SYSREQ key starting with Windows 3.0. VK_INSERT 2Dh VK_DELETE 2Eh VK_HELP 2Fh VK_0 30h VK_1 31h VK_2 32h VK_3 33h VK_4 34h VK_5 35h VK_6 36h VK_7 37h VK_8 38h VK_9 39h 3Ah though 40h undefined. VK_A 41h VK_B 42h VK_C 43h VK_D 44h VK_E 45h VK_F 46h VK_G 47h VK_H 48h VK_I 49h VK_J 4Ah VK_K 4Bh VK_L 4Ch VK_M 4Dh VK_N 4Eh VK_O 4Fh VK_P 50h VK_Q 51h VK_R 52h VK_S 53h VK_T 54h VK_U 55h VK_V 56h VK_W 57h VK_X 58h VK_Y 59h VK_Z 5Ah 5Bh through 5Fh undefined. VK_NUMPAD0 60h VK_NUMPAD1 61h VK_NUMPAD2 62h VK_NUMPAD3 63h VK_NUMPAD4 64h VK_NUMPAD5 65h VK_NUMPAD6 66h VK_NUMPAD7 67h VK_NUMPAD8 68h VK_NUMPAD9 69h VK_MULTIPLY 6Ah VK_ADD 6Bh VK_SEPARATER 6Ch Never generated by keyboard driver. VK_SUBTRACT 6Dh VK_DECIMAL 6Eh VK_DIVIDE 6Fh VK_F1 70h VK_F2 71h VK_F3 72h VK_F4 73h VK_F5 74h VK_F6 75h VK_F7 76h VK_F8 77h VK_F9 78h VK_F10 79h VK_F11 7Ah VK_F12 7Bh VK_F13 7Ch VK_F14 7Dh VK_F15 7Eh VK_F16 7Fh VK_F17 80h VK_F18 81h VK_F19 82h VK_F20 83h VK_F21 84h VK_F22 85h VK_F23 86h VK_F24 87h 88h through 8Fh unassigned. VK_NUMLOCK 090h NUMLOCK on all keyboards. VK_OEM_SCROLL 091h SCROLL LOCK on all keyboards. 92h through B9h unassigned. VK_OEM_1 0BAh Punctuation. VK_OEM_PLUS 0BBh Punctuation. VK_OEM_COMMA 0BCh Punctuation. VK_OEM_MINUS 0BDh Punctuation. VK_OEM_PERIOD 0BEh Punctuation. VK_OEM_2 0BFh Punctuation. VK_OEM_3 0C0h Punctuation. C1h through DAh unassigned. VK_OEM_4 0DBh Punctuation. VK_OEM_5 0DCh Punctuation. VK_OEM_6 0DDh Punctuation. VK_OEM_7 0DEh Punctuation. VK_OEM_8 0DFh Punctuation. VK_F17 0E0h F17 key on Olivetti extended keyboard (internal use only). VK_F18 0E1h F18 key on Olivetti extended keyboard (internal use only). VK_OEM_102 0E2h < or | on IBM-compatible 102 enhanced keyboard (non-U.S.). VK_ICO_HELP 0E3h Help key on Olivetti extended keyboard (internal use only). VK_ICO_00 0E4h 00 key on Olivetti extended keyboard (internal use only). E5h unassigned. VK_ICO_CLEAR 0E6h Olivetti extended keyboard (internal use only). E7h and E8h unassigned. VK_OEM_RESET 0E9H Only used by Nokia. VK_OEM_JUMP 0EAH Only used by Nokia. VK_OEM_PA1 0EBH Only used by Nokia. VK_OEM_PA2 0ECH Only used by Nokia. VK_OEM_PA3 0EDH Only used by Nokia. VK_OEM_WSCTRL 0EEH Only used by Nokia. VK_OEM_CUSEL 0EFH Only used by Nokia. VK_OEM_ATTN 0F0H Only used by Nokia. VK_OEM_FINNISH 0F1H Only used by Nokia. VK_OEM_COPY 0F2H Only used by Nokia. VK_OEM_AUTO 0F3H Only used by Nokia. VK_OEM_ENLW 0F4h Only used by Nokia. VK_OEM_BACKTAB 0F5h Only used by Nokia. VK_ATTN 0F6H VK_CRSEL 0F7H VK_EXSEL 0F8H VK_EREOF 0F9H VK_PLAY 0FAH VK_ZOOM 0FBH VK_NONAME 0FCH VK_PA1 0FDH VK_OEM_CLEAR 0FEH
worldy 2014-07-10
  • 打赏
  • 举报
回复
WM_KEYDOWN The WM_KEYDOWN message is posted to the window with the keyboard focus when a nonsystem key is pressed. A nonsystem key is a key that is pressed when the alt key is not pressed. WM_KEYDOWN nVirtKey = (int) wParam; // virtual-key code lKeyData = lParam; // key data Parameters nVirtKey Value of wParam. Specifies the virtual-key code of the nonsystem key. lKeyData Value of lParam. Specifies the repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag, as shown in the following table. Value Description 0–15 Specifies the repeat count for the current message. The value is the number of times the keystroke is auto-repeated as a result of the user holding down the key. If the keystroke is held long enough, multiple messages are sent. However, the repeat count is not cumulative. 16–23 Specifies the scan code. The value depends on the original equipment manufacturer (OEM). 24 Specifies whether the key is an extended key, such as the right-hand alt and ctrl keys that appear on an enhanced 101- or 102-key keyboard. The value is 1 if it is an extended key; otherwise, it is 0. 25–28 Reserved; do not use. 29 Specifies the context code. The value is always 0 for a WM_KEYDOWN message. 30 Specifies the previous key state. The value is 1 if the key is down before the message is sent, or it is 0 if the key is up. 31 Specifies the transition state. The value is always 0 for a WM_KEYDOWN message. Return Values An application should return zero if it processes this message. Default Action If the f10 key is pressed, the DefWindowProc function sets an internal flag. When DefWindowProc receives the WM_KEYUP message, the function checks whether the internal flag is set and, if so, sends a WM_SYSCOMMAND message to the top-level window. The wParam parameter of the message is set to SC_KEYMENU. Remarks Because of the autorepeat feature, more than one WM_KEYDOWN message may be posted before a WM_KEYUP message is posted. The previous key state (bit 30) can be used to determine whether the WM_KEYDOWN message indicates the first down transition or a repeated down transition. For enhanced 101- and 102-key keyboards, extended keys are the right alt and ctrl keys on the main section of the keyboard; the ins, del, home, end, page up, page down and arrow keys in the clusters to the left of the numeric keypad; and the divide (/) and enter keys in the numeric keypad. Other keyboards may support the extended-key bit in the lKeyData parameter. QuickInfo Windows NT: Requires version 3.1 or later. Windows: Requires Windows 95 or later. Windows CE: Requires version 1.0 or later. Header: Declared in winuser.h. See Also
赵4老师 2014-07-10
  • 打赏
  • 举报
回复
仅供参考
//The _getch function reads a single character from the console without echoing.
//Function can not be used to read CTRL+Break.
//When reading a function key or an arrow key,
//_getch must be called twice; the first call returns 0 or 0xE0,
//and the second call returns the actual key code.
#include <conio.h>
#include <windows.h>
void main() {
    unsigned short k;

    while (1) {
        Sleep(100);
        k=getch();
        if (27==k) break;//按Esc键退出
        if (0==k||0xe0==k) k|=getch()<<8;//非字符键
        cprintf("%04x pressed.\r\n",k);
    }
}

2,640

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 硬件/系统
社区管理员
  • 硬件/系统社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧