0

In a method i'm receiving vk_key's as just a int, nothing more is provided.

I need to forward this, which looks like:

parent.postEvent(new KeyEvent(null, parent.millis(), KeyEvent.TYPE, modiefiers, c_key, vk_key));

The problem is there are also things like VK_UP, VK_DOWN etc. Where normally I need to do a cast: char c_key = (char)vk_key;, this is incorrect for VK_UP etc. cause the cast will make the up key a '&' for example. For those cases the c_key should be set to KeyEvent.CHAR_UNDEFINED.

What I do now is this:

switch (vk_key) {
    case VK_UP: 
    case VK_LEFT:
    case VK_RIGHT:
    case VK_DOWN:
    c_key = CHAR_UNDEFINED;
    break;
}

But this does not feel like the right way to do it. Ideally there would have been a static method like static public char getCharForKey(int vk_key), which would return CHAR_UNDEFINED where required. But as always oracle makes things way harder then they should be.

So is there any easy way to figure out if something should be CHAR_UNDEFINED or not?

Hakan Dilek
  • 2,178
  • 2
  • 23
  • 35
clankill3r
  • 9,146
  • 20
  • 70
  • 126
  • Where are you getting a naked `vk_key` code from? Usually you only get them from `KeyEvent`s... Also note: if the component is `null` the [KeyEvent](https://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyEvent.html#KeyEvent(java.awt.Component,%20int,%20long,%20int,%20int,%20char)) constructor will throw an `IllegalArgumentException` – BeyelerStudios Jul 18 '20 at 11:42
  • Simply receive the event instead of creating one, then call this on the event: https://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyEvent.html#getKeyChar() – Doga Oruc Jul 18 '20 at 11:47
  • @BeyelerStudios I get them from a terminal that is set in a mode that no return key is required, I use `System.in.read()` and map them to the right `VK_KEY` where required. For example when I press the up key, the terminal will create the sequence `27 91 65` and I know that is VK_UP. – clankill3r Jul 18 '20 at 14:46
  • `char c_key = (char)vk_key;` is *not* valid. There are no guarantees that a keycode will be equal to its corresponding char value. The only way to obtain a char value from a key press is by listening for [keyTyped](https://docs.oracle.com/en/java/javase/14/docs/api/java.desktop/java/awt/event/KeyListener.html#keyTyped(java.awt.event.KeyEvent)) events and reading the value with [getKeyChar](https://docs.oracle.com/en/java/javase/14/docs/api/java.desktop/java/awt/event/KeyEvent.html#getKeyChar()). – VGR Jul 18 '20 at 15:25
  • @VGR I can't listen for keyTyped events, cause it is the terminal that has focus and not the java application. – clankill3r Jul 18 '20 at 16:54
  • If the terminal sends 65 by itself, that is not a key code; it’s already a character. The VK_* constants in KeyEvent only apply to KeyEvents. You are free to map escape sequences to control keys; that is in fact what terminfo databases do. – VGR Jul 18 '20 at 17:33
  • Does this answer your question? [How to read a single char from the console in Java (as the user types it)?](https://stackoverflow.com/questions/1066318/how-to-read-a-single-char-from-the-console-in-java-as-the-user-types-it) – BeyelerStudios Jul 18 '20 at 18:36
  • @BeyelerStudios I have no problem reading the chars. I only have problems forwarding the char I readed to processing (processing.org) as if it where a real event. – clankill3r Jul 19 '20 at 09:31
  • Since you're using `System.in.read()` you're not getting `VK`-KeyCodes (see [read](https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read())). As it stands your question poses an [xy-problem](https://meta.stackexchange.com/a/66378). What you want to achieve was already asked in [How to read a single char from the console in Java](https://stackoverflow.com/questions/1066318/how-to-read-a-single-char-from-the-console-in-java-as-the-user-types-it) look at the proposed solutions like [this one](https://stackoverflow.com/a/30008252/3426025) or particularize your question. – BeyelerStudios Jul 19 '20 at 19:07

0 Answers0