Keyboard Input Reference (ctrl+A响应例子)

文档来自于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.
BlockInputBlocks keyboard and mouse input events from reaching applications.
EnableWindowEnables or disables mouse and keyboard input to the specified window or control.
GetActiveWindowRetrieves the window handle to the active window attached to the calling thread's message queue.
GetAsyncKeyStateDetermines 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.
GetFocusRetrieves the handle to the window that has the keyboard focus.
GetKeyboardLayoutRetrieves the active input locale identifier for the specified thread.
GetKeyboardLayoutListRetrieves the input locale identifiers corresponding to the current set of input locales in the system.
GetKeyboardLayoutNameRetrieves the name of the active input locale identifier.
GetKeyboardStateCopies the status of the 256 virtual keys to the specified buffer.
GetKeyNameTextRetrieves a string that represents the name of a key.
GetKeyStateRetrieves the status of the specified virtual key.
GetLastInputInfoRetrieves the time of the last input event.
IsWindowEnabledDetermines whether the specified window is enabled for mouse and keyboard input.
keybd_eventSynthesizes a keystroke.
LoadKeyboardLayoutLoads a new input locale identifier.
MapVirtualKeyTranslates (maps) a virtual-key code into a scan code or character value, or translates a scan code into a virtual-key code.
MapVirtualKeyExTranslates (maps) a virtual-key code into a scan code or character value, or translates a scan code into a virtual-key code.
OemKeyScanMaps OEM ASCII codes 0 through 0x0FF into the OEM scan codes and shift states.
RegisterHotKeyDefines a system-wide hot key.
SendInputSynthesizes keystrokes, mouse motions, and button clicks.
SetActiveWindowActivates a window.
SetFocusSets the keyboard focus to the specified window.
SetKeyboardStateCopies a 256-byte array of keyboard key states into the calling thread's keyboard input-state table.
ToAsciiTranslates the specified virtual-key code and keyboard state to the corresponding character or characters.
ToAsciiExTranslates the specified virtual-key code and keyboard state to the corresponding character or characters.
ToUnicodeTranslates the specified virtual-key code and keyboard state to the corresponding Unicode character or characters.
ToUnicodeExTranslates the specified virtual-key code and keyboard state to the corresponding Unicode character or characters.
UnloadKeyboardLayoutUnloads an input locale identifier.
UnregisterHotKeyFrees a hot key previously registered by the calling thread.
VkKeyScanTranslates a character to the corresponding virtual-key code and shift state for the current keyboard.
VkKeyScanExTranslates a character to the corresponding virtual-key code and shift state.

 

GetKeyState

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.

Keyboard Input Structures

The following structures are used for keyboard input.

  • HARDWAREINPUT
  • INPUT
  • KEYBDINPUT
  • LASTINPUTINFO
  • MOUSEINPUT

 

Keyboard Input Messages

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

 

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. (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.


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.

ValueDescription
0–15Specifies 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–23Specifies the scan code. The value depends on the original equipment manufacturer (OEM).
24Specifies 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–28Reserved; do not use.
29Specifies the context code. The value is always 0 for a WM_KEYDOWN message.
30Specifies 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.
31Specifies 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键值除外。

Virtual-Key Codes

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
(hexadecimal)

Mouse or keyboard equivalent
VK_LBUTTON01Left mouse button
VK_RBUTTON02Right mouse button
VK_CANCEL03Control-break processing
VK_MBUTTON04Middle mouse button (three-button mouse)
 05 
 06 
 07 
VK_BACK08 
VK_TAB09 
-0A–0BReserved
.........................

完整的表请参考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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值