Converting non-control ASCII chars to KeyEvent key codes?

S

softwarepearls_com

Is there any simple way?

Looking at KeyEvent.VK_A to keyEvent.VK_Z's contiguous range lets us
map letters quite easily, but I'm after broader input, namely all non-
control ASCII characters.

I'm after an API like

public int toKeyEventKeyCode(byte asciiChar)

??
 
M

Mark Space

softwarepearls_com said:
Is there any simple way?

Looking at KeyEvent.VK_A to keyEvent.VK_Z's contiguous range lets us
map letters quite easily, but I'm after broader input, namely all non-
control ASCII characters.

I'm after an API like

public int toKeyEventKeyCode(byte asciiChar)

??

I haven't tried this but KeyEvent does take a char in two of its
constructors....
 
S

softwarepearls_com

I haven't tried this but KeyEvent does take a char in two of its
constructors....

Sure.. but if you check the source, you'll see that the char is simply
stored, and nowhere does it form the input for some kind of logic that
I'm after (in KeyEvent that is).

Actually, the API that I'm after would need to return an array of key
codes because some chars would map to more than a single keycode. E.g.
all capitals would map to VK_SHIFT plus the letter key code.
 
J

John B. Matthews

softwarepearls_com said:
Sure.. but if you check the source, you'll see that the char is simply
stored, and nowhere does it form the input for some kind of logic that
I'm after (in KeyEvent that is).

A KeyEvent holds information about which key on a keyboard was pressed,
released or typed. To translate a keyCode to a Unicode character, you
need the modifiers, as is done by getKeyChar() for KEY_TYPED events.
Indeed, the constructor throws an exception for certain meaningless
combinations.

IUUC, you want the converse: the KeyEvent that would be generated for a
given character. Sadly, there's nothing named getGlyphOnKeyCap(); I
guess a keyboard full of little LCDs would be a might pricey and perhaps
too fragile.
Actually, the API that I'm after would need to return an array of key
codes because some chars would map to more than a single keycode. E.g.
all capitals would map to VK_SHIFT plus the letter key code.

Would it help to explain what you're trying to accomplish?
 
S

softwarepearls_com

A KeyEvent holds information about which key on a keyboard was pressed,
released or typed. To translate a keyCode to a Unicode character, you
need the modifiers, as is done by getKeyChar() for KEY_TYPED events.
Indeed, the constructor throws an exception for certain meaningless
combinations.

IUUC, you want the converse: the KeyEvent that would be generated for a
given character. Sadly, there's nothing named getGlyphOnKeyCap(); I
guess a keyboard full of little LCDs would be a might pricey and perhaps
too fragile.


Would it help to explain what you're trying to accomplish?

Sure, no problem since this bit is going to be OSS-ed.

I've written a class KeyboardRemoteControl which .. hang on.. copy-
paste from javadocs:

/
**********************************************************************************************
* Miniature TCP-based server giving arbitrary external programs
access to a {@link Robot}
* instance's native keyboard event injecting capabilities.
* <P>
* You can embed a {@link KeyboardRemoteControl} into any application
(or plugin) to allow
* primitive keyboard-driven "scripting" of your application. For
applications which provide
* good support for keyboard shortcuts (eg for menu item actions),
this means you can expose
* most of your application's functionality via this back door.
* <P>
* The TCP port used to publish the service is configurable (any port
within 1024..65535
* range).
* <P>
* The {@link KeyboardRemoteControl} protocol is very simple, and
consists only of the
* following ASCII commands sent to the server.
* <UL>
* <LI> C
&lt;keycode&gt;,&lt;keycode&gt;,&lt;keycode&gt;,...&lt;EOL&gt;
* <LI> P &lt;keycode&gt;&lt;EOL&gt;
* <LI> R &lt;keycode&gt;&lt;EOL&gt;
* <LI> S &lt;string&gt;&lt;EOL&gt;
* <LI> E&lt;EOL&gt;
* </UL>
* C = Chord (multiple keys pressed simultaneously), eg to send CTRL-
C<BR>
* P = Press key<BR>
* R = Release key<BR>
* S = String, eg to send a string of plain characters like a
filename<BR>
* E = ENTER key
* <P>
* &lt;keycode&gt; is a valid, integer key code (as defined by {@link
KeyEvent}),
* &lt;string&gt; is any String of characters that should be
translated into a sequence of
* Press/Release events. &lt;EOL&gt; is either LF, CR, or a CR-LF
pair.
* <P>
* The server never sends back any information to the connecting
client (not to welcome a
* client, nor to acknowledge received commands).
*
* @author Software Pearls BVBA

*********************************************************************************************/

The only thing not yet workig is the S packet, ie being able to send a
whole String of characters to be fed to the Robot instance. Getting
that to work requires the OT requirements..
 
J

John B. Matthews

softwarepearls_com said:
A KeyEvent holds information about which key on a keyboard was
pressed, released or typed. To translate a keyCode to a Unicode
character, you need the modifiers, as is done by getKeyChar() for
KEY_TYPED events. Indeed, the constructor throws an exception for
certain meaningless combinations.

IUUC, you want the converse: the KeyEvent that would be generated
for a given character. Sadly, there's nothing named
getGlyphOnKeyCap(); I guess a keyboard full of little LCDs would be
a might pricey and perhaps too fragile.


Would it help to explain what you're trying to accomplish?

Sure, no problem since this bit is going to be OSS-ed.

I've written a class KeyboardRemoteControl which .. hang on.. copy-
paste from javadocs: [...]
* C = Chord (multiple keys pressed simultaneously), eg to send CTRL-C<BR>
* P = Press key<BR>
* R = Release key<BR>
* S = String, eg to send a string of plain characters like a filename<BR>
* E = ENTER key [...]

The only thing not yet workig is the S packet, ie being able to send a
whole String of characters to be fed to the Robot instance. Getting
that to work requires the OT requirements.

I'm not sure what to make of the phrase "multiple keys pressed
simultaneously." I'd think ETX, for example, would require a sequence:

keyPress(VK_CONTROL)
keyPress(VK_C)
keyRelease(VK_C)
keyRelease(VK_CONTROL)

A similar sequence for 'A' would involve VK_SHIFT and VK_A, while 'a'
would require only VK_A. A String would comprise a series of such
sequences.
 
S

softwarepearls_com

Sure, no problem since this bit is going to be OSS-ed.
I've written a class KeyboardRemoteControl which .. hang on.. copy-
paste from javadocs: [...]
* C = Chord (multiple keys pressed simultaneously), eg to send CTRL-C<BR>
* P = Press key<BR>
* R = Release key<BR>
* S = String, eg to send a string of plain characters like a filename<BR>
* E = ENTER key [...]

The only thing not yet workig is the S packet, ie being able to send a
whole String of characters to be fed to the Robot instance. Getting
that to work requires the OT requirements.

I'm not sure what to make of the phrase "multiple keys pressed
simultaneously." I'd think ETX, for example, would require a sequence:

keyPress(VK_CONTROL)
keyPress(VK_C)
keyRelease(VK_C)
keyRelease(VK_CONTROL)

A similar sequence for 'A' would involve VK_SHIFT and VK_A, while 'a'
would require only VK_A. A String would comprise a series of such
sequences.

You are right, only the P, R and E packets are necessary, but to have
something a bit more user-friendly, the S and C packet types are
provided to avoid having to break down everything into primitive key
presses and releases.

It looks like the convert logic I'm after also needs to take into
account the keyboard type. Eg. QWERTY requires SHIFT to be pressed to
get (eg) '@', whereas AZERRTY doesn't. I'm starting to see why
KeyEvent doesn't give me the kind of functionality I'm after.. it's
not trivial to implement well.
 
J

John B. Matthews

softwarepearls_com said:
 softwarepearls_com said:
<bf9c1fba-7a1f-4b3f-a507-56e02ab04...@o40g2000prn.googlegroups.com>,
softwarepearls_com wrote:
Is there any simple way?
Looking at KeyEvent.VK_A to keyEvent.VK_Z's contiguous
range lets us map letters quite easily, but I'm after
broader input, namely all non- control ASCII characters.
I'm after an API like
public int toKeyEventKeyCode(byte asciiChar)
I haven't tried this but KeyEvent does take a char in two
of its constructors....
Sure.. but if you check the source, you'll see that the char
is simply stored, and nowhere does it form the input for some
kind of logic that I'm after (in KeyEvent that is).
A KeyEvent holds information about which key on a keyboard was
pressed, released or typed. To translate a keyCode to a Unicode
character, you need the modifiers, as is done by getKeyChar()
for KEY_TYPED events. Indeed, the constructor throws an
exception for certain meaningless combinations.
IUUC, you want the converse: the KeyEvent that would be
generated for a given character. Sadly, there's nothing named
getGlyphOnKeyCap(); I guess a keyboard full of little LCDs
would be a might pricey and perhaps too fragile.
Actually, the API that I'm after would need to return an
array of key codes because some chars would map to more than
a single keycode. E.g. all capitals would map to VK_SHIFT
plus the letter key code.
Would it help to explain what you're trying to accomplish?
Sure, no problem since this bit is going to be OSS-ed.
I've written a class KeyboardRemoteControl which .. hang on..
copy- paste from javadocs: [...]
* C = Chord (multiple keys pressed simultaneously), eg to send CTRL-C<BR>
* P = Press key<BR>
* R = Release key<BR>
* S = String, eg to send a string of plain characters like a filename<BR>
* E = ENTER key [...]

The only thing not yet workig is the S packet, ie being able to
send a whole String of characters to be fed to the Robot
instance. Getting that to work requires the OT requirements.

I'm not sure what to make of the phrase "multiple keys pressed
simultaneously." I'd think ETX, for example, would require a
sequence:

keyPress(VK_CONTROL)
keyPress(VK_C)
keyRelease(VK_C)
keyRelease(VK_CONTROL)

A similar sequence for 'A' would involve VK_SHIFT and VK_A, while
'a' would require only VK_A. A String would comprise a series of
such sequences.

You are right, only the P, R and E packets are necessary, but to have
something a bit more user-friendly, the S and C packet types are
provided to avoid having to break down everything into primitive key
presses and releases.

It looks like the convert logic I'm after also needs to take into
account the keyboard type. Eg. QWERTY requires SHIFT to be pressed to
get (eg) '@', whereas AZERRTY doesn't. I'm starting to see why
KeyEvent doesn't give me the kind of functionality I'm after.. it's
not trivial to implement well.

Egad! I knew of several Latin variations, but I'd not heard of AZERTY;
and there's scores more for non-Latin scripts:

<http://en.wikipedia.org/wiki/Keyboard_layout>
 
S

softwarepearls_com

Is there any simple way?

Looking at KeyEvent.VK_A to keyEvent.VK_Z's contiguous range lets us
map letters quite easily, but I'm after broader input, namely all non-
control ASCII characters.

I'm after an API like

public int toKeyEventKeyCode(byte asciiChar)

??

For those who are interested, I started a project on SourceForge
called eclipsekbdrc (Eclipse Keyboard Remote Control) which contains a
utility class to do the conversion. Took me a while to write it, so I
figure others may find it useful..
 
M

Mark Space

softwarepearls_com said:
For those who are interested, I started a project on SourceForge
called eclipsekbdrc (Eclipse Keyboard Remote Control) which contains a
utility class to do the conversion. Took me a while to write it, so I
figure others may find it useful..

Cool. Is it really only for Eclipse or would it work in any Java program?
 
Joined
Feb 26, 2009
Messages
1
Reaction score
0
Use KeyStroke!

this.putValue(MNEMONIC_KEY, KeyStroke.getKeyStroke("N").getKeyCode() );
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,685
Members
48,796
Latest member
Greg L.

Latest Threads

Top