接著上一章講,此時修改myview的dispatchkeyevent的keydown事件的返回值
@override
public boolean dispatchkeyevent(keyevent event)
return super.dispatchkeyevent(event);
}
執行結果
com.example.keytest i/wangjiasheng_mainactivity: dispatchkeyevent_action_down
com.example.keytest i/wangjiasheng_viewgroup1: dispatchkeyevent_action_down
com.example.keytest i/wangjiasheng_myview: dispatchkeyevent_action_down
com.example.keytest i/wangjiasheng_mainactivity: dispatchkeyevent_action_up
com.example.keytest i/wangjiasheng_viewgroup1: dispatchkeyevent_action_up
com.example.keytest i/wangjiasheng_myview: dispatchkeyevent_action_up
com.example.keytest i/wangjiasheng_myview: onkeyup_action_up
com.example.keytest i/wangjiasheng_mainactivity: onkeyup_action_up
可以看出dispatchkeyevent的keydown事件返回true,myview的onkeydwon不會執行了。對於onkeyup事件不影響
@override
public boolean dispatchkeyevent(keyevent event)
return super.dispatchkeyevent(event);
}
執行結果
com.example.keytest i/wangjiasheng_mainactivity: dispatchkeyevent_action_down
com.example.keytest i/wangjiasheng_viewgroup1: dispatchkeyevent_action_down
com.example.keytest i/wangjiasheng_myview: dispatchkeyevent_action_down
com.example.keytest i/wangjiasheng_mainactivity: onkeydown_action_down
com.example.keytest i/wangjiasheng_mainactivity: dispatchkeyevent_action_up
com.example.keytest i/wangjiasheng_viewgroup1: dispatchkeyevent_action_up
com.example.keytest i/wangjiasheng_myview: dispatchkeyevent_action_up
com.example.keytest i/wangjiasheng_myview: onkeyup_action_up
com.example.keytest i/wangjiasheng_mainactivity: onkeyup_action_up
可以看出當dispatchkeyevent的onkeydown事件返回false,myview的onkeydown也不會執行,但是activity的onkeydown執行了
此時恢復dispathkeyevent的返回值,修改onkeydown的返回值為true
@override
public boolean onkeydown(int keycode, keyevent event)
return super.onkeydown(keycode, event);
}
執行結果
com.example.keytest i/wangjiasheng_mainactivity: dispatchkeyevent_action_down
com.example.keytest i/wangjiasheng_viewgroup1: dispatchkeyevent_action_down
com.example.keytest i/wangjiasheng_myview: dispatchkeyevent_action_down
com.example.keytest i/wangjiasheng_myview: onkeydown_action_down
com.example.keytest i/wangjiasheng_mainactivity: dispatchkeyevent_action_up
com.example.keytest i/wangjiasheng_viewgroup1: dispatchkeyevent_action_up
com.example.keytest i/wangjiasheng_myview: dispatchkeyevent_action_up
com.example.keytest i/wangjiasheng_myview: onkeyup_action_up
com.example.keytest i/wangjiasheng_mainactivity: onkeyup_action_up
可以看出當onkeydown的返回值為true,事件將不會再向上傳遞了,那返回值為false呢?
@override
public boolean onkeydown(int keycode, keyevent event)
return super.onkeydown(keycode, event);
}
執行結果
com.example.keytest i/wangjiasheng_mainactivity: dispatchkeyevent_action_down
com.example.keytest i/wangjiasheng_viewgroup1: dispatchkeyevent_action_down
com.example.keytest i/wangjiasheng_myview: dispatchkeyevent_action_down
com.example.keytest i/wangjiasheng_myview: onkeydown_action_down
com.example.keytest i/wangjiasheng_mainactivity: onkeydown_action_down
com.example.keytest i/wangjiasheng_mainactivity: dispatchkeyevent_action_up
com.example.keytest i/wangjiasheng_viewgroup1: dispatchkeyevent_action_up
com.example.keytest i/wangjiasheng_myview: dispatchkeyevent_action_up
com.example.keytest i/wangjiasheng_myview: onkeyup_action_up
com.example.keytest i/wangjiasheng_mainactivity: onkeyup_action_up
當myview的onkeydown返回值為false,事件傳遞沒有變化 按鍵事件處理
當按鍵來臨時可能會有三種動作 按鍵的動作狀態可以通過event.getaction 方法來獲取。一般只要down和up兩種行為。from 當然按鍵也有很多標誌位,可以通過event.getflags 方法來獲取按鍵的標誌位。在明白了這些之後,在分析按鍵之前首先要是弄清楚按鍵按下時的action,標誌...
qt按鍵事件處理機制分析
本為以qt2.3.12作為原始碼進行分析。下面是乙個簡單的顯示乙個按鍵,並將按鍵鏈結到退出操作的程式,本文將一步一步分析,qt是如何實現此操作的。qpushbutton是繼承於qbutton的,我們可以到qbutton.cpp中檢視click 事件是如何產生的。qt2 src widgets qbu...
Input的按鍵事件
onkeydown 當按鍵按下會觸發的事件,當需要特定的按鍵按下,可以使用if event.keycode 這裡的?就是 keycode 的碼,可以檢視 根據對照表,找到自己需要的keycode碼 填入?處 例如if event.keycode 13 jump keycode碼的 13是回車,當這個...