不知各位有沒有自己實做過類似TextBox或是需要完全接收鍵盤按建的自訂元件…
假如有, 應該都會碰到一個問題… 就是接收不到方向鍵… 這不知道是微軟的好意還是故意的(我想應該是好意的 ^^)…
當你將你寫好的UserControl放到WinForm後執行他…
你會發覺就算你去override OnKeyDown也接收不到那四個該死的方向鍵…
這是因為你沒有另外去override另一個Method… 那就是IsInputKey…
只要把回傳值改為true… 這樣你就可以攔截到鍵盤上所有的按鍵值了…
而IsInputChar與IsInputKey的意思是一樣的…
差別是在一個攔截按鍵(IsInputKey), 而另一個是攔截字元(IsInputChar)…
各位有空的話去查一下這兩個method…
大致上的用意是… 要不要讓接收到的按鍵(或字元)傳回容器去… 另一個解釋就是要不要攔截這個按鍵(或字元)
true: 攔截; false: 不攔截, 傳到容器去
[csharp] protected override bool IsInputKey(Keys keyData) {
/* 這裡需回傳true, 否則無法接收到方向鍵
* MSDN:
* Call the IsInputKey method to determine whether the key specified by the keyData
* parameter is an input key that the control wants.
* This method is called during window message preprocessing to determine whether
* the specified input key should be preprocessed or sent directly to the control.
* If IsInputKey returns true, the specified key is sent directly to the control.
* If IsInputKey returns false, the specified key is preprocessed and only sent to
* the control if it is not consumed by the preprocessing phase.
* Keys that are preprocessed include the TAB, RETURN, ESCAPE, and the UP ARROW,
* DOWN ARROW, LEFT ARROW, and RIGHT ARROW keys. */
return true;
}[/csharp]

最後修改日期: 2006-05-29

作者

留言

撰寫回覆或留言

發佈留言必須填寫的電子郵件地址不會公開。