Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
983 views
in Technique[技术] by (71.8m points)

key - Converting a Char into Java KeyEvent KeyCode

I am writing a basic program that asks the user to type in a String, and I am trying to use a Robot (from java.awt.Robot)that will type this message back into another document after a delay. The Problem I have now is that I need to convert whatever I get from message.charAt(i) to a KeyEvent.VK_[insert Char] KeyCode. Is there a better way to do what I am trying to do? I suppose I could always just have a massive switch statement that gets the appropriate KeyCode but I was hoping there would be a more elegant way. My first thought, having done python for awhile, was to make a string "KeyEvent.VK_" + message.charAt(i) and convert that to code somehow, but I think the only way to do that is using Reflection which is discouraged.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If I have a unicode character and all I want is to know the keystroke for it, then I use

int keyCode = java.awt.event.KeyEvent.getExtendedKeyCodeForChar(charCode);

I've never needed another, more complicated way to do this.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...