Hello Guest

[BUG] Keyboard.getEventCharacter() if Keyboard.getEventKeyState is false

  • 4 Replies
  • 7448 Views
Hello LWJGL community,

I am currently working on some LWJGL tutorials and I ran across a weird bug with the Keyboard.getEventCharacter() method. When called after checking that a key was released (Keyboard.getEventKeyState == false), it seems to return a blank char that terminates string building. What I mean by this is that it does not return the Char that specifies the key released AND it pre-terminates any string that was being built with the method call inside. For instance:

Code: [Select]
while(Keyboard.next()) {
   if(Keyboard.getEventKeyState()) {
      System.out.println("Keyboard Event: \"" + Keyboard.getEventCharacter() + "\" was pressed!");
   } else {
      System.out.println("Keyboard Event: \"" + Keyboard.getEventCharacter() + "\" was released!");
   }
}

The output goes something like this:

Keyboard Event: "a" was pressed!
Keyboard Event: "
Keyboard Event: "d" was pressed!
Keyboard Event: "

Whenever i query the Keyboard.getEventCharacter() upon key releasing, there seems to be no char data available in the buffer to read. Thus a null is returned (I guess) or perhaps some special char? Either way, it pre-emptively cuts off the string but does not raise an Exception or throw a runtime error. It might have something to do with my keyboard or operating system, so here is some helpful info about my computer:

OS: Kubuntu 11.10 x64 (Linux Kernel: 3.0.0-16-generic)
Keyboard: Dell Keyboard KB212-B (Swiss DE Layout) via USB
Editor: Eclipse EE Version: 3.7.0 Build id: I20110613-1736
LWJGL: lwjgl-2.8.3

I am just wondering if this is intended behaviour; i.e. when a key is released, you cannot know which key it is; or if this is a problem of my OS/Hardware/Software; or a genuine bug of some sort.

Thanks for your patience!  :)

EDIT: Fixed a bug in the above Java code.
« Last Edit: March 22, 2012, 18:58:09 by KatalystGames »

I think this is the expected behavior. It probably returns the NULL character which certrain programming languages (such as C) often use to mark the end of strings. So that's probably the reason why you see only part of your string in the terminal window.

A single key may create a number of different characters ("a" or "A" depending on whether shift was pressed, for example). So it's not possible to decide which key was responsible for the character "A". More often than not you would look for the key that generated the event - and that is provided by the Keyboard class, even when the key is released.
Download Cultris II, the fastest Tetris clone from http://gewaltig.net/

I can understand what you mean dr_evil. I know that LWJGL cannot know which key specifically was pressed on any particular keyboard... however I would think that it should still be able to access the bytecode description of the key in the buffer of keyboard events. What I find strange is that it is registered into the event buffer but has no label? Makes me wonder how LWJGL can then send back an Int signal specific for a particular character upon key release if the bytebuffer of the keyboard contains a null??? Somehow, I think it is an error either in the description of the method Keyboard.getEventCharacter() in the javadoc/apidoc which does not mention this border case; or it is some kind of particular occurence for my setup. Have you tried replicating the error? Would be cool to see if this happens on other OS's.

Quick update! I went on to a friends Windows 7 computer and tried out the above snippet of code. Turns out under windows it will not cut the string short! However, the character returned is a blank space! This is the sample output from the first post but under windows:

Keyboard Event: "a" was pressed!
Keyboard Event: " " was released!
Keyboard Event: "d" was pressed!
Keyboard Event: " " was released!

However when i just copy-pasted the output from the eclipse console into the post edit on this forum.... it cut off at the first blank return statement like so:

Keyboard Event: "a" was pressed!
Keyboard Event: "

What the hell is going on here? Is there some hidden linux carriage return in there that is causing the text to cut off in the browser but not in the eclipse console???

What do you mean by "bytecode description of the key"? Your answer makes me think that you didn't fully grasp the concept of the keyboard interface. I don't know if this helps:
1. getEventKey() returns a value which can be compared to the constants in the Keyboard class or passed to the getKeyName method.
2. getEventCharacter() represents a character that was entered. Now that doesn't have to correspond to key presses. For example, when entering certain asian languages (Japanese for example), people have to press multiple keys for a single character to appear. You'd use this method *only* when a player had to enter text (for a chat, for example). This shouldn't be used to move a character in a game around, for example.

Your copy/paste "problem" is, as I wrote before, caused by the NULL byte. Read more about it on wikipedia: http://en.wikipedia.org/wiki/Null_character. getEventCharacter() returns NULL when the user didn't enter a character by pressing a key (for example when releasing a key or when you only press the SHIFT key).
Download Cultris II, the fastest Tetris clone from http://gewaltig.net/