kowala's home

kowala's home
這裡是我的學習筆記,陸續增加中。
http://kowala21.blogspot.com

2012-07-07

wxDev C++ ,如何從鍵盤參數 lParam 取出 scan code。

想使用鍵盤來模擬按鍵,如果有使用到鍵盤事件,這個參數 lParam 就有必要去了解一下。

依據 MSDN 說明如下:
wParam [in]    Type: WPARAM
    The virtual-key code of the key that generated the keystroke message.

lParam [in]    Type: LPARAM
    The repeat count, scan code, extended-key flag, context code,
    previous key-state flag, and transition-state flag. For more information about
    the lParam parameter, see Keystroke Message Flags. The following table
    describes the bits of this value.

    Bits    Description
    0-15    The repeat count. The value is the number of times the keystroke is repeated as a result of the user's holding down the key.
    16-23    The scan code. The value depends on the OEM.
    24    Indicates whether the key is an extended key, such as a function key or a key on the numeric keypad. The value is 1 if the key is an extended key; otherwise, it is 0.
    25-28    Reserved.
    29    The context code. The value is 1 if the ALT key is down; otherwise, it is 0.
    30    The previous key state. The value is 1 if the key is down before the message is sent; it is 0 if the key is up.
    31    The transition state. The value is 0 if the key is being pressed and 1 if it is being released.


位元定義圖[1]


lParam 它是長度 DWORD 的數值,各位元有其代表意義[2]。現在我們關心的是位元 16-23,依定義知道它是代表 scan code,下面示範如何取出它。



ex: 取值'a' = 0xc01e0001,16-23 The scan code.(0x1e=30)

1100 0000 0001 1110 0000 0000 0000 0001 = 0xc01e0001

先與 0xFF0000 AND
0000 0000 1111 1111 0000 0000 0000 0000 = 0x00FF0000
1100 0000 0001 1110 0000 0000 0000 0001 = 0xc01e0001

結果
0000 0000 0001 1110 0000 0000 0000 0000

再右旋16次可得
0000 0000 0000 0000 0000 0000 0001 1010

取 scan code
scankey=(((DWORD)(lParam) & 0xFF0000) >> 16);

運算後
scankey= 0000 0000 0000 0000 0000 0000 0001 1010

WORD xx=(WORD)scankey;
xx = 0000 0000 0001 1110
HIBYTE(xx)   = 0x00 = 0000 0000
LOBYTE(xx) = 0x1e = 0001 1110

資料長度
BYTE  =  8 bits
WORD  = 16 bits
DWORD = 32 bits

參考資料
[1]. About Keyboard Input
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646267%28v=vs.85%29.aspx
[2]. WM_CHAR
http://msdn.microsoft.com/en-us/library/aa453873

沒有留言:

張貼留言

請提供您的寶貴意見 ;-)