文档来自于MSDN Documentation 2001。
The following elements are associated with keyboard input:
- Keyboard Input Functions
- Keyboard Input Structures
- Keyboard Input Messages
Keyboard Input Functions
The following functions are used to receive and process keyboard input.
ActivateKeyboardLayout | Sets the input locale identifier for the calling thread or the current process. |
BlockInput | Blocks keyboard and mouse input events from reaching applications. |
EnableWindow | Enables or disables mouse and keyboard input to the specified window or control. |
GetActiveWindow | Retrieves the window handle to the active window attached to the calling thread's message queue. |
GetAsyncKeyState | 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. |
GetFocus | Retrieves the handle to the window that has the keyboard focus. |
GetKeyboardLayout | Retrieves the active input locale identifier for the specified thread. |
GetKeyboardLayoutList | Retrieves the input locale identifiers corresponding to the current set of input locales in the system. |
GetKeyboardLayoutName | Retrieves the name of the active input locale identifier. |
GetKeyboardState | Copies the status of the 256 virtual keys to the specified buffer. |
GetKeyNameText | Retrieves a string that represents the name of a key. |
GetKeyState | Retrieves the status of the specified virtual key. |
GetLastInputInfo | Retrieves the time of the last input event. |
IsWindowEnabled | Determines whether the specified window is enabled for mouse and keyboard input. |
keybd_event | Synthesizes a keystroke. |
LoadKeyboardLayout | Loads a new input locale identifier. |
MapVirtualKey | Translates (maps) a virtual-key code into a scan code or character value, or translates a scan code into a virtual-key code. |
MapVirtualKeyEx | Translates (maps) a virtual-key code into a scan code or character value, or translates a scan code into a virtual-key code. |
OemKeyScan | Maps OEM ASCII codes 0 through 0x0FF into the OEM scan codes and shift states. |
RegisterHotKey | Defines a system-wide hot key. |
SendInput | Synthesizes keystrokes, mouse motions, and button clicks. |
SetActiveWindow | Activates a window. |
SetFocus | Sets the keyboard focus to the specified window. |
SetKeyboardState | Copies a 256-byte array of keyboard key states into the calling thread's keyboard input-state table. |
ToAscii | Translates the specified virtual-key code and keyboard state to the corresponding character or characters. |
ToAsciiEx | Translates the specified virtual-key code and keyboard state to the corresponding character or characters. |
ToUnicode | Translates the specified virtual-key code and keyboard state to the corresponding Unicode character or characters. |
ToUnicodeEx | Translates the specified virtual-key code and keyboard state to the corresponding Unicode character or characters. |
UnloadKeyboardLayout | Unloads an input locale identifier. |
UnregisterHotKey | Frees a hot key previously registered by the calling thread. |
VkKeyScan | Translates a character to the corresponding virtual-key code and shift state for the current keyboard. |
VkKeyScanEx | Translates a character to the corresponding virtual-key code and shift state. |
The GetKeyState function retrieves the status of the specified virtual key. The status specifies whether the key is up, down, or toggled (on, off—alternating each time the key is pressed).
SHORT GetKeyState( int nVirtKey // virtual-key code );
Parameters
nVirtKey
[in] Specifies a virtual key. If the desired virtual key is a letter or digit (A through Z, a through z, or 0 through 9), nVirtKey must be set to the ASCII value of that character. For other keys, it must be a virtual-key code.
If a non-English keyboard layout is used, virtual keys with values in the range ASCII A through Z and 0 through 9 are used to specify most of the character keys. For example, for the German keyboard layout, the virtual key of value ASCII O (0x4F) refers to the "o" key, whereas VK_OEM_1 refers to the "o with umlaut" key.
Return Values
The return value specifies the status of the specified virtual key, as follows:
- If the high-order bit is 1, the key is down; otherwise, it is up. (如果返回值的最高位为1,则键被按下。否则键被释放)
- If the low-order bit is 1, the key is toggled. A key, such as the CAPS LOCK key, is toggled if it is turned on. The key is off and untoggled if the low-order bit is 0. A toggle key's indicator light (if any) on the keyboard will be on when the key is toggled, and off when the key is untoggled.
Remarks
The key status returned from this function changes as a thread reads key messages from its message queue. The status does not reflect the interrupt-level state associated with the hardware. Use the GetAsyncKeyState function to retrieve that information.
An application calls GetKeyState in response to a keyboard-input message. This function retrieves the state of the key when the input message was generated.
To retrieve state information for all the virtual keys, use the GetKeyboardState function. (一次性得到所有256个键的状态)
An application can use the virtual-key code constants VK_SHIFT, VK_CONTROL, and VK_MENU as values for the nVirtKey parameter. This gives the status of the SHIFT, CTRL, or ALT keys without distinguishing between left and right. An application can also use the following virtual-key code constants as values for nVirtKey to distinguish between the left and right instances of those keys.
VK_LSHIFT
VK_RSHIFT
VK_LCONTROL
VK_RCONTROL
VK_LMENU
VK_RMENU
These left- and right-distinguishing constants are available to an application only through the GetKeyboardState, SetKeyboardState, GetAsyncKeyState, GetKeyState, and MapVirtualKey functions.
The following structures are used for keyboard input.
- HARDWAREINPUT
- INPUT
- KEYBDINPUT
- LASTINPUTINFO
- MOUSEINPUT
The following messages are used to receive and process keyboard input.
- WM_ACTIVATE
- WM_APPCOMMAND
- WM_CHAR
- WM_DEADCHAR
- WM_GETHOTKEY
- WM_HOTKEY
- WM_KEYDOWN
- WM_KEYUP
- WM_KILLFOCUS
- WM_SETFOCUS
- WM_SETHOTKEY
- WM_SYSCHAR
- WM_SYSDEADCHAR
- WM_SYSKEYDOWN
- WM_SYSKEYUP
- WM_UNICHAR
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. (ALT 键与其他键组合按的话,一般触发WM_SYSKEYDOWN消息,这里的键不包括键盘上的字符键)
A window receives this message through its WindowProc function.
LRESULT CALLBACK WindowProc( HWND hwnd, // handle to window UINT uMsg, // WM_KEYDOWN WPARAM wParam, // virtual-key code LPARAM lParam // key data );
Parameters
wParam
Specifies the virtual-key code of the nonsystem key.
lParamSpecifies 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 autorepeated 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 zero if the key is up. 31 Specifies the transition state. The value is always zero for a WM_KEYDOWN message.
Return Values
An application should return zero if it processes this message.
Remarks
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.
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 lParam parameter.
Windows 2000/XP: Applications must pass wParam to TranslateMessage without altering it at all.
扩展一个知识点在此
从设备来的键值会先被映射成Virtual-key,不能映射的Character键值除外。
The following table shows the symbolic constant names, hexadecimal values, and mouse or keyboard equivalents for the virtual-key codes used by the system. The codes are listed in numeric order.
Symbolic constant name | Value | Mouse or keyboard equivalent |
VK_LBUTTON | 01 | Left mouse button |
VK_RBUTTON | 02 | Right mouse button |
VK_CANCEL | 03 | Control-break processing |
VK_MBUTTON | 04 | Middle mouse button (three-button mouse) |
05 | ||
06 | ||
07 | ||
VK_BACK | 08 | |
VK_TAB | 09 | |
- | 0A–0B | Reserved |
............. | ...... | ...... |
完整的表请参考MSDN文档。
只是说明这张表中有对于键盘上所有键的VIRTUAL CODE。从这张表中可以看到Character系统中不会定义对定的Virtual值。因为键盘发出的值就作为其键值。(virtual code,character code)
表中0-9,A-Z字符的值就是其ASC||码值。
Example one:
作用:我的程序中用到的一小段代码。窗口内响应键盘上的Ctrl+A的操作。
PreTranslateMessage()表示还没有把键盘上被按的键值翻译成字符值前的操作。
.h中重载PreTranslateMessage()函数。
public:
virtual BOOL PreTranslateMessage(MSG* pMsg);
.cpp中定义其实现。
// 响应键盘Ctrl+A全选操作
BOOL PhotoViewDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if(pMsg->message == WM_KEYDOWN)
{
// Ctrl键一直被按着在;
// 此时键盘上A键被按下,判断此时WM_KEYDOWN时候的wParam的Virtual code;
if((pMsg->wParam == 'A') && ((GetKeyState(VK_LCONTROL)<0)||(GetKeyState(VK_RCONTROL)<0)))
{
int nCount = m_PhotosListCtrl.GetItemCount();
for(int i = 0;i<nCount;++i)
{
m_PhotosListCtrl.SetItemState(i,LVIS_SELECTED,LVIS_SELECTED);
}
return TRUE;
}
}
return CDialog::PreTranslateMessage(pMsg);
}
后记:个人感觉这段代码也可以写到WM_KEYDOWN中去。但是没有试验。有兴趣的人可以试验下。
Example Two:
作用:ListCtrl里面的item可以被键盘的DELETE键删除;
直接利用CLASSWIZARD添加LISTCTRL的KEYDOWN响应函数。
.h中
public:
afx_msg void OnLvnKeydownPhotosList(NMHDR *pNMHDR, LRESULT *pResult);
.cpp中
BEGIN_MESSAGE_MAP(PhotoViewDlg, CDialog)
ON_WM_PAINT()
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_BUTTON_PVCLOSE, &PhotoViewDlg::OnBnClickedButtonPvclose)
ON_NOTIFY(NM_DBLCLK, IDC_PHOTOS_LIST, &PhotoViewDlg::OnNMDblclkPhotosList)
ON_NOTIFY(NM_RCLICK, IDC_PHOTOS_LIST, &PhotoViewDlg::OnNMRclickPhotosList)
ON_COMMAND(ID_LISTVIEWMENU_DELETEFILE, &PhotoViewDlg::OnListviewmenuDeletefile)
ON_COMMAND(ID_LISTVIEWMENU_SENDMAIL, &PhotoViewDlg::OnListviewmenuSendmail)
ON_NOTIFY(LVN_KEYDOWN, IDC_PHOTOS_LIST, &PhotoViewDlg::OnLvnKeydownPhotosList)
END_MESSAGE_MAP()
......
// 响应键盘上的DELETE键的删除操作
void PhotoViewDlg::OnLvnKeydownPhotosList(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMLVKEYDOWN pLVKeyDow = reinterpret_cast<LPNMLVKEYDOWN>(pNMHDR);
// TODO: Add your control notification handler code here
if(pLVKeyDow->wVKey == VK_DELETE)
{
OnListviewmenuDeletefile(); // 自定义响应操作函数;
}
*pResult = 0;
}