[英]Mnemonics in GWT
I would like to create as Java Swing Mnemonics with GWT . 我想用GWT创建Java Swing助记符 。 But I don't know how to figure it out. 但我不知道如何解决这个问题。 I have googled for it but I didn't fond any sample codes for it . 我已经google了,但我并不喜欢它的任何示例代码。 I want to bind some keyboard shortcut keys on my buttons. 我想在我的按钮上绑定一些键盘快捷键。 How can I achieve it ? 我怎样才能实现它? Any suggestions would be really appreciated ! 任何建议将非常感谢!
In general you can handle global keyboard shortcusts using a NativePreviewHandler. 通常,您可以使用NativePreviewHandler处理全局键盘shortcust。 An example of this can you see here: 您可以在此处看到此示例:
NativePreviewHandler nativePreviewHandler = new NativePreviewHandler() {
@Override
public void onPreviewNativeEvent(NativePreviewEvent event) {
if (event.getTypeInt() != Event.ONKEYDOWN) {
return;
}
final NativeEvent nativeEvent = event.getNativeEvent();
final boolean altKey = nativeEvent.getAltKey();
final boolean ctrlKey = nativeEvent.getCtrlKey();
if(altKey && ctrlKey && nativeEvent.getKeyCode() == 'A') {
// Do Something
}
}
};
Event.addNativePreviewHandler(nativePreviewHandler);
But as far as I klnow, there's no generic way build into GWT to handle some kind of Action that is bound to a button/Menu as well as a keyboard shortcut. 但据我所知,没有通用的方法构建GWT来处理绑定到按钮/菜单以及键盘快捷键的某种Action。 You will have to implement such an abstraction by yourselves. 你必须自己实现这样的抽象。
I hope this code will help you. 我希望这段代码可以帮到你。 Here we are adding a key down handler on document element. 这里我们在文档元素上添加一个键向下处理程序。
RootPanel.get().addDomHandler(new KeyDownHandler() {
@Override
public void onKeyDown(KeyDownEvent event) {
if (event.isControlKeyDown()) {
char ch = (char) event.getNativeKeyCode();
if (ch == 's' || ch == 'S') {
// do operation for Ctrl+S
} else if (ch == 'c' || ch == 'C') {
// do operation for Ctrl+C
}
// add more or use switch case
}
}
}, KeyDownEvent.getType());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.