您可以使用Node.addEventHandler方法将 KeyEvent EventHandler 添加到任何节点(在本例中为您的 WebView),如果您处理 EventType KeyEvent.KEY_TYPED您可以使用KeyEvent.getCharacter方法获取键入的unicode字符。看这个例子:
WebView myWebView = new WebView();
myWebView.addEventHandler(KeyEvent.KEY_TYPED,
new EventHandler<KeyEvent>()
{
@Override
public void handle(KeyEvent event)
{
System.out.println("Unicode character typed: "+event.getCharacter());
switch (event.getCharacter())
{
case "á":
System.out.println("Typed accented a");
break;
case "é":
System.out.println("Typed accented e");
break;
case "í":
System.out.println("Typed accented i");
break;
case "ó":
System.out.println("Typed accented o");
break;
case "ú":
System.out.println("Typed accented u");
break;
default:
System.out.println("Typed other key " + event.getCode());
break;
}
}
});
如果您想比较忽略语言环境、大写、小写等的不同字符串,您可能需要查看Collator类。如果您想考虑“á”和“a”相等,它可能会很有用。
祝你好运!