{
@Override
public void eventDispatched(AWTEvent event)
{
if(event instanceof KeyEvent)
{
KeyEvent ke=(KeyEvent)event
if(ke.getID()==KeyEvent.KEY_RELEASED/*键盘按下后释放时触发*/&&ke.getKeyCode()==KeyEvent.VK_ESCAPE/*你的键值KeyEvent.XXX*/)
{
//你的处理方法在这,JWindow的引用.怎么样?
}
}
else if(event instanceof MouseEvent)
{
MouseEvent me=(MouseEvent)event
if(me.getID()==MouseEvent.MOUSE_RELEASED/*鼠标按下后释放时触发*/)
{
//你的处理方法在这
}
}
//好些呢,想监听啥就监听啥
}
}, AWTEvent.MOUSE_EVENT_MASK+AWTEvent.MOUSE_MOTION_EVENT_MASK+AWTEvent.MOUSE_WHEEL_EVENT_MASK+AWTEvent.KEY_EVENT_MASK)
如果你的窗口隐藏了,还想监听,那可能需要JNA库的支持
其实添加多侦听就可以了,其实默认的是侦听 stage的,就是说键盘事件是针对stage,而当你鼠标在其它按钮上触发事件的时候,你的焦点就转换为这个按钮对象了,并不是stage,自然当你再按键盘的时候,不会有事件发行。也自然侦听不到,如果你想在任意时候都能侦听到键盘事件,就只有把所有的按钮及影片剪辑都注册为侦听器对象就可以了,让他们都做为事件的发送者,执行的只有一个函数就可以了。1.在lichee/linux-3.0/include/linux/input.h文件可以查看底层驱动的按键编码#define KEY_F1 59
#define KEY_F2 60
#define KEY_F3 61
#define KEY_F4 62
#define KEY_F5 63
#define KEY_F6 64
#define KEY_F7 65
#define KEY_F8 66
#define KEY_F9 67
#define KEY_F10 68
2.在android4.0/frameworks/base/core/java/android/view/KeyEvent.java文件可以查看上层驱动的按键编码
/** Key code constant: F1 key. */
public static final int KEYCODE_F1 = 131
/** Key code constant: F2 key. */
public static final int KEYCODE_F2 = 132
/** Key code constant: F3 key. */
public static final int KEYCODE_F3 = 133
/** Key code constant: F4 key. */
public static final int KEYCODE_F4 = 134
/** Key code constant: F5 key. */
public static final int KEYCODE_F5 = 135
/** Key code constant: F6 key. */
public static final int KEYCODE_F6 = 136
/** Key code constant: F7 key. */
public static final int KEYCODE_F7 = 137
/** Key code constant: F8 key. */
public static final int KEYCODE_F8 = 138
/** Key code constant: F9 key. */
public static final int KEYCODE_F9 = 139
/** Key code constant: F10 key. */
public static final int KEYCODE_F10 = 140
3.在/android4.0/frameworks/base/data/keyboards/Generic.kl文件里面有底层到上层的映射,将这个文件定制到自己的系统android4.0/out/target/product/crane-m1003h6/system/usr/keylayout目录下就可以了在上层收到USB键盘F1-F10的消息了。
key 59F1
key 60F2
key 61F3
key 62F4
key 63F5
key 64F6
key 65F7
key 66F8
key 67F9
key 68F10
4.在/android4.0/frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindow.java文件的onKeyDown,onKeyUp函数截获F1-F10的消息,用自己定义的广播消息广播出去应用就可以在后台或者前台都可以接收到按键按下或者释放的广播消息了。
case KeyEvent.KEYCODE_F1: {
//Log.e("zkliu","---------------------------test F1---------------------------")
getContext().sendBroadcast(new Intent("com.android.action.F1_KEYDOWN"))
return true
}
case KeyEvent.KEYCODE_F2: {
//Log.e("zkliu","---------------------------test F2---------------------------")
getContext().sendBroadcast(new Intent("com.android.action.F2_KEYDOWN"))
return true
}
case KeyEvent.KEYCODE_F3: {
//Log.e("zkliu","---------------------------test F3---------------------------")
getContext().sendBroadcast(new Intent("com.android.action.F3_KEYDOWN"))
return true
}
case KeyEvent.KEYCODE_F4: {
//Log.e("zkliu","---------------------------test F4---------------------------")
getContext().sendBroadcast(new Intent("com.android.action.F4_KEYDOWN"))
return true
}
case KeyEvent.KEYCODE_F5: {
//Log.e("zkliu","---------------------------test F5---------------------------")
getContext().sendBroadcast(new Intent("com.android.action.F5_KEYDOWN"))
return true
}
case KeyEvent.KEYCODE_F6: {
//Log.e("zkliu","---------------------------test F6---------------------------")
getContext().sendBroadcast(new Intent("com.android.action.F6_KEYDOWN"))
return true
}
case KeyEvent.KEYCODE_F7: {
//Log.e("zkliu","---------------------------test F7---------------------------")
getContext().sendBroadcast(new Intent("com.android.action.F7_KEYDOWN"))
return true
}
case KeyEvent.KEYCODE_F8: {
//Log.e("zkliu","---------------------------test F8---------------------------")
getContext().sendBroadcast(new Intent("com.android.action.F8_KEYDOWN"))
return true
}
case KeyEvent.KEYCODE_F9: {
//Log.e("zkliu","---------------------------test F9---------------------------")
getContext().sendBroadcast(new Intent("com.android.action.F9_KEYDOWN"))
return true
}
case KeyEvent.KEYCODE_F10: {
//Log.e("zkliu","---------------------------test F10---------------------------")
getContext().sendBroadcast(new Intent("com.android.action.F10_KEYDOWN"))
return true
}
case KeyEvent.KEYCODE_F1: {
//Log.e("zkliu","---------------------------test F1---------------------------")
getContext().sendBroadcast(new Intent("com.android.action.F1_KEYUP"))
return true
}
case KeyEvent.KEYCODE_F2: {
//Log.e("zkliu","---------------------------test F2---------------------------")
getContext().sendBroadcast(new Intent("com.android.action.F2_KEYUP"))
return true
}
case KeyEvent.KEYCODE_F3: {
//Log.e("zkliu","---------------------------test F3---------------------------")
getContext().sendBroadcast(new Intent("com.android.action.F3_KEYUP"))
return true
}
case KeyEvent.KEYCODE_F4: {
//Log.e("zkliu","---------------------------test F4---------------------------")
getContext().sendBroadcast(new Intent("com.android.action.F4_KEYUP"))
return true
}
case KeyEvent.KEYCODE_F5: {
//Log.e("zkliu","---------------------------test F5---------------------------")
getContext().sendBroadcast(new Intent("com.android.action.F5_KEYUP"))
return true
}
case KeyEvent.KEYCODE_F6: {
//Log.e("zkliu","---------------------------test F6---------------------------")
getContext().sendBroadcast(new Intent("com.android.action.F6_KEYUP"))
return true
}
case KeyEvent.KEYCODE_F7: {
//Log.e("zkliu","---------------------------test F7---------------------------")
getContext().sendBroadcast(new Intent("com.android.action.F7_KEYUP"))
return true
}
case KeyEvent.KEYCODE_F8: {
//Log.e("zkliu","---------------------------test F8---------------------------")
getContext().sendBroadcast(new Intent("com.android.action.F8_KEYUP"))
return true
}
case KeyEvent.KEYCODE_F9: {
//Log.e("zkliu","---------------------------test F9---------------------------")
getContext().sendBroadcast(new Intent("com.android.action.F9_KEYUP"))
return true
}
case KeyEvent.KEYCODE_F10: {
//Log.e("zkliu","---------------------------test F10---------------------------")
getContext().sendBroadcast(new Intent("com.android.action.F10_KEYUP"))
return true
}
应用程序注册接收相应的广播消息即可接收到键盘输入消息。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)