kowala's home
http://kowala21.blogspot.com
前一篇 wxDev C++ 使用 winFrame 視窗+測試鍵盤 wxKeyEvent 之1
我們必須先取出按鍵值,然後解析它。這裡的目標是要把全部的按鍵都抓到,先區分按鍵為可列印按鍵(printable character)或是擴展按鍵(special key),可以這樣來區分[1]。
int code=event.GetModifiers();//組合鍵狀態
int keyCode = event.GetKeyCode();//ascii 鍵值
if ( wuChar != WXK_NONE && code == 0){ //一般鍵
wxLogMessage("printable key '%c'", event.GetUnicodeKey());
}else{//組合鍵
wxLogMessage("special key '%c'", event.GetUnicodeKey());
}
在一般鍵裡頭,包含有命令鍵,如 Enter,TAB,BACK等等,它並不可視,所以要加一個 switch () 來把它們分離出來,我使用一個字串( TCHAR str[] )來幫忙顯示結果,在wxDev C++之中,似乎是使用 unicode,這意味著,使用中文會有亂碼問題[2]。
int code=event.GetModifiers();//組合鍵狀態
int keyCode = event.GetKeyCode();//ascii 鍵值
TCHAR str[]={0};
if ( wuChar != WXK_NONE && code == 0){ //一般鍵
switch (keyCode){
case WXK_BACK://8
wsprintf(str,L"Key : %s",L"BACK");
MessageBox(0,str,L"Normal Key",MB_ICONINFORMATION|MB_OK);
break;
case WXK_TAB://9
wsprintf(str,L"Key : %s",L"TAB");
MessageBox(0,str,L"Normal Key",MB_ICONINFORMATION|MB_OK);
break;
......
default:
wsprintf(str,L"Key : %c",keyCode);
MessageBox(0,str,L"Normal Key",MB_OK);
}
}else{//組合鍵
wxLogMessage("special key '%c'", event.GetUnicodeKey());
}
執行結果
到這裡,我們已經處理了一般按鍵,接著要來處理組合鍵及特別鍵,像是 Shift+key,Ctrl+Key,PageUP,F1,F2...
組合鍵說明(code 的組合)
Alt=1
Ctrl=2
Shift=4
所以
Ctrl+Alt = 2+1 = 3
Shift+Ctrl = 4+2 = 6
Shift+Ctrl+Alt = 4+2+1 = 7
...
組合鍵要判斷兩次,要判斷 code 的組合,再取 keyCode 鍵值。
wxDevC++已經定義了一些虛擬鍵值,請參考 Type KeyEvent [3] .
為了保持簡潔,把它獨立出來,另外寫個 chkExtKey() 如下。
void chkExtKey(int keyCode){//擴展鍵+組合鍵
TCHAR str[100]={0};
switch (keyCode){
case 324://NumLock 0 = 324
_stprintf(str,L"code = %d, key = %c",keyCode,'0');
MessageBox(0,str,L"Extend Key",MB_ICONINFORMATION|MB_OK);
break;
case WXK_NUMPAD1://NumLock 1 = 325
_stprintf(str,L"code = %d, key = %c",keyCode,'1');
MessageBox(0,str,L"Extend Key",MB_ICONINFORMATION|MB_OK);
break;
..............
default:
_stprintf(str,L"(key,code)=(%c,%d)",keyCode,keyCode);
MessageBox(0,str,L"Extend Key",MB_ICONINFORMATION|MB_OK);
}
}
執行結果
使用方法
int code=event.GetModifiers();//組合鍵狀態
int keyCode = event.GetKeyCode();//ascii 鍵值
TCHAR str[]={0};
if ( wuChar != WXK_NONE && code == 0){ //一般鍵
switch (keyCode){...}
}else{//組合鍵
switch (code){ //code 的組合
//(Shift,Ctrl,Alt)=(4,2,1)
case 1:
MessageBox(0,L"Alt",L"Func Key",MB_ICONINFORMATION|MB_OK);
break;
case 2:
MessageBox(0,L"Ctrl",L"Func Key",MB_ICONINFORMATION|MB_OK);
break;
case 3:
MessageBox(0,L"Ctrl+Alt",L"Func Key",MB_ICONINFORMATION|MB_OK);
break;
case 4:
MessageBox(0,L"Shift",L"Func Key",MB_ICONINFORMATION|MB_OK);
break;
case 5:
MessageBox(0,L"Shift+Alt",L"Func Key",MB_ICONINFORMATION|MB_OK);
break;
case 6:
MessageBox(0,L"Shift+Ctrl",L"Func Key",MB_ICONINFORMATION|MB_OK);
break;
case 7:
MessageBox(0,L"Shift+Ctrl+Alt",L"Func Key",MB_ICONINFORMATION|MB_OK);
break;
}
chkExtKey(keyCode);//擴展鍵
}case 1:
MessageBox(0,L"Alt",L"Func Key",MB_ICONINFORMATION|MB_OK);
break;
case 2:
MessageBox(0,L"Ctrl",L"Func Key",MB_ICONINFORMATION|MB_OK);
break;
case 3:
MessageBox(0,L"Ctrl+Alt",L"Func Key",MB_ICONINFORMATION|MB_OK);
break;
case 4:
MessageBox(0,L"Shift",L"Func Key",MB_ICONINFORMATION|MB_OK);
break;
case 5:
MessageBox(0,L"Shift+Alt",L"Func Key",MB_ICONINFORMATION|MB_OK);
break;
case 6:
MessageBox(0,L"Shift+Ctrl",L"Func Key",MB_ICONINFORMATION|MB_OK);
break;
case 7:
MessageBox(0,L"Shift+Ctrl+Alt",L"Func Key",MB_ICONINFORMATION|MB_OK);
break;
}
chkExtKey(keyCode);//擴展鍵
完整代碼
/*
* kbTestFrmKeyDown
*/
void chkExtKey(int keyCode){//擴展鍵
TCHAR str[100]={0};
switch (keyCode){
case 324://NumLock 0 = 324
_stprintf(str,L"code = %d, key = %c",keyCode,'0');
MessageBox(0,str,L"Extend Key",MB_ICONINFORMATION|MB_OK);
break;
case WXK_NUMPAD1://NumLock 1 = 325
_stprintf(str,L"code = %d, key = %c",keyCode,'1');
MessageBox(0,str,L"Extend Key",MB_ICONINFORMATION|MB_OK);
break;
case WXK_NUMPAD2://NumLock 2 = 326
_stprintf(str,L"code = %d, key = %c",keyCode,'2');
MessageBox(0,str,L"Extend Key",MB_ICONINFORMATION|MB_OK);
break;
case WXK_NUMPAD3://NumLock 3 = 327
_stprintf(str,L"code = %d, key = %c",keyCode,'3');
MessageBox(0,str,L"Extend Key",MB_ICONINFORMATION|MB_OK);
break;
case WXK_NUMPAD4://NumLock 4 = 328
_stprintf(str,L"code = %d, key = %c",keyCode,'4');
MessageBox(0,str,L"Extend Key",MB_ICONINFORMATION|MB_OK);
break;
case WXK_NUMPAD5://NumLock 5 = 329
_stprintf(str,L"code = %d, key = %c",keyCode,'5');
MessageBox(0,str,L"Extend Key",MB_ICONINFORMATION|MB_OK);
break;
case WXK_NUMPAD6://NumLock 6 = 330
_stprintf(str,L"code = %d, key = %c",keyCode,'6');
MessageBox(0,str,L"Extend Key",MB_ICONINFORMATION|MB_OK);
break;
case WXK_NUMPAD7://NumLock 7 = 331
_stprintf(str,L"code = %d, key = %c",keyCode,'7');
MessageBox(0,str,L"Extend Key",MB_ICONINFORMATION|MB_OK);
break;
case WXK_NUMPAD8://NumLock 8 = 332
_stprintf(str,L"code = %d, key = %c",keyCode,'8');
MessageBox(0,str,L"Extend Key",MB_ICONINFORMATION|MB_OK);
break;
case WXK_NUMPAD9://NumLock 9 = 333
_stprintf(str,L"code = %d, key = %c",keyCode,'9');
MessageBox(0,str,L"Extend Key",MB_ICONINFORMATION|MB_OK);
break;
case WXK_F1://F1
_stprintf(str,L"code = %d, key = %s",keyCode,L"F1");
MessageBox(0,str,L"Extend Key",MB_ICONINFORMATION|MB_OK);
break;
case WXK_F2://F2
_stprintf(str,L"code = %d, key = %s",keyCode,L"F2");
MessageBox(0,str,L"Extend Key",MB_ICONINFORMATION|MB_OK);
break;
case WXK_NUMLOCK://NUMLOCK
_stprintf(str,L"code = %d, key = %s",keyCode,L"NUMLOCK");
MessageBox(0,str,L"Extend Key",MB_ICONINFORMATION|MB_OK);
break;
case WXK_PAGEUP://PAGEUP
_stprintf(str,L"code = %d, key = %s",keyCode,L"PAGEUP");
MessageBox(0,str,L"Extend Key",MB_ICONINFORMATION|MB_OK);
break;
case WXK_SPECIAL1://SPECIAL1
_stprintf(str,L"code = %d, key = %s",keyCode,L"SPECIAL1");
MessageBox(0,str,L"Extend Key",MB_ICONINFORMATION|MB_OK);
break;
default:
_stprintf(str,L"(key,code)=(%c,%d)",keyCode,keyCode);
MessageBox(0,str,L"Extend Key",MB_ICONINFORMATION|MB_OK);
}
}
void kbTestFrm::kbTestFrmKeyDown(wxKeyEvent& event){
int code=event.GetModifiers();//組合鍵狀態,(Shift,Ctrl,Alt)=(4,2,1)
int keyCode = event.GetKeyCode();//ascii 鍵值
wxUint32 wuChar=event.GetUnicodeKey();//unicode鍵值
TCHAR str[]={0};
if ( wuChar != WXK_NONE && code == 0){//一般鍵
switch (keyCode){
case WXK_BACK://8
wsprintf(str,L"Key : %s",L"BACK");
MessageBox(0,str,L"Normal Key",MB_ICONINFORMATION|MB_OK);
break;
case WXK_TAB://9
wsprintf(str,L"Key : %s",L"TAB");
MessageBox(0,str,L"Normal Key",MB_ICONINFORMATION|MB_OK);
break;
case WXK_RETURN://13
wsprintf(str,L"Key : %s",L"RETURN");
MessageBox(0,str,L"Normal Key",MB_ICONINFORMATION|MB_OK);
break;
case WXK_ESCAPE://27
wsprintf(str,L"Key : %s",L"ESCAPE");
MessageBox(0,str,L"Normal Key",MB_ICONINFORMATION|MB_OK);
break;
case WXK_SPACE://32
wsprintf(str,L"Key : %s",L"SPACE");
MessageBox(0,str,L"Normal Key",MB_ICONINFORMATION|MB_OK);
break;
case WXK_DELETE://127
wsprintf(str,L"Key : %s",L"DELETE");
MessageBox(0,str,L"Normal Key",MB_ICONINFORMATION|MB_OK);
break;
default:
wsprintf(str,L"Key : %c",keyCode);
MessageBox(0,str,L"Normal Key",MB_OK);
}
}else{//組合鍵 (Shift,Ctrl,Alt)=(4,2,1)
switch (code){//code 的組合 //(Shift,Ctrl,Alt)=(4,2,1)
case 1:
MessageBox(0,L"Alt",L"Func Key",MB_ICONINFORMATION|MB_OK);
break;
case 2:
MessageBox(0,L"Ctrl",L"Func Key",MB_ICONINFORMATION|MB_OK);
break;
case 3:
MessageBox(0,L"Ctrl+Alt",L"Func Key",MB_ICONINFORMATION|MB_OK);
break;
case 4:
MessageBox(0,L"Shift",L"Func Key",MB_ICONINFORMATION|MB_OK);
break;
case 5:
MessageBox(0,L"Shift+Alt",L"Func Key",MB_ICONINFORMATION|MB_OK);
break;
case 6:
MessageBox(0,L"Shift+Ctrl",L"Func Key",MB_ICONINFORMATION|MB_OK);
break;
case 7:
MessageBox(0,L"Shift+Ctrl+Alt",L"Func Key",MB_ICONINFORMATION|MB_OK);
break;
}
chkExtKey(keyCode);//擴展鍵
}
}
參考
[1].wxKeyEvent Class Reference
http://docs.wxwidgets.org/trunk/classwx_key_event.html
[2].wxDev C++ 開始使用它來建立視窗(2)
http://kowala21.blogspot.tw/2012/05/wxdev-c-2.html
[3].Type KeyEvent
http://wxpython.org/docs/api/wx.KeyEvent-class.html
沒有留言:
張貼留言
請提供您的寶貴意見 ;-)