You can use keyboard in two modes: simple mode and event mode. Simple mode is easier so better start from it. Event mode allows you to detect not only when a user pressed a key but also when they released a key.
We start from “simple mode” and then show how to use event mode.
Simple mode.
To work with keyboard you have to create an object of Keyboard class passing the window as the argument.
val keyboard = Keyboard(wnd)
Keyboard keeps a queue from information about all pressed keys. You can get information about one key which was pressed the first by calling getPressedKey method:
val key = keyboard.getPressedKey()
After you got the key it is erased from the keyboard queue. And next call of getPressedKey returns the key which was pressed after the previous one. If the keyboard queue is empty, meaning there are not pressed keys to process, getPressedKey returns null. Here is an example showing how you can process pressed keys from the keyboard:
import com.anysolo.toyGraphics.* fun main() { val wnd = Window(800, 600) val keyboard = Keyboard(wnd) while(true) { do { val key = keyboard.getPressedKey() if (key != null) { println(key) println("key: " + key.code) } } while(key != null) sleep(1) } }
To understand why you need the inner loop “do” try to change sleep(1) to sleep(10). If you press several keys you see how they processed by that loop when 10 seconds of sleep is finished.
getPressedKey() returns an object of class Key. This object contains all information about what key was pressed and which modifiers (Ctrl, Shift, Alt) was pressed with it. The most important piece of information is “code” property. It contains an Int representing code of the key. You can compare it to one of the known codes from KeyCodes.
Here is an example waiting for Escape key to be pressed. It also shows how you can check key modifiers like shift, alt or control.
package demos import com.anysolo.toyGraphics.* fun main() { val wnd = Window(800, 600) val keyboard = Keyboard(wnd) while(true) { do { val key = keyboard.getPressedKey() if (key != null) { println(key) println("key: " + key.code) if(key.code == KeyCodes.ESCAPE) println("Escape key. Does not matter shift or not shift") if(key.code == KeyCodes.ESCAPE && key.isShift) println("Escape key + shift") if(key.code == KeyCodes.ESCAPE && !key.isShift) println("Escape key. No shift") // An empty line before output from the next loop iteration println() } } while(key != null) sleep(1) } }
KeyCodes contains only special key codes, like function keys, arrow and so on. If you want to check if some alphabetical key was pressed you can do like this example shows you:
package demos import com.anysolo.toyGraphics.* fun main() { val wnd = Window(800, 600) val keyboard = Keyboard(wnd) while(true) { do { val key = keyboard.getPressedKey() if (key != null) { println(key) println("key: " + key.code) if(key.code == 'Q'.toInt()) println("'Q' key. Does not matter Alt or not Alt") if(key.code == 'Q'.toInt() && key.isAlt) println("'Q' key + Alt") if(key.code == 'Q'.toInt() && !key.isAlt) println("'Q' key. No Alt") // An empty line before output from the next loop iteration println() } } while(key != null) sleep(1) } }
Event mode
To use the keyboard in event mode you have to pass eventMode=true parameter to Keyboard object.
val keyboard = Keyboard(wnd, eventMode=true)
Instead of calling getPressedKey() function you should call getEvent().
val keyEvent = keyboard.getEvent()
If the keyboard queue is empty it returns null.
Below is a full example of detecting pressing and releasing.
import com.anysolo.toyGraphics.* fun main() { val wnd = Window(800, 600) val keyboard = Keyboard(wnd, eventMode=true) while(true) { do { val keyEvent = keyboard.getEvent() if (keyEvent != null) { println(keyEvent) // Some key was pressed if(keyEvent.isPressed) { if (keyEvent.code == 'Q'.toInt()) break if (keyEvent.code == KeyCodes.LEFT) println("Left is pressed") if (keyEvent.code == KeyCodes.RIGHT) println("Right is pressed") if (keyEvent.code == KeyCodes.ALT) println("ALT is pressed") if (keyEvent.code == KeyCodes.SHIFT) println("SHIFT is pressed") if (keyEvent.code == KeyCodes.CTRL) println("CTRL is pressed") } else { if (keyEvent.code == KeyCodes.LEFT) println("Left is released") if (keyEvent.code == KeyCodes.RIGHT) println("Right is released") if (keyEvent.code == KeyCodes.ALT) println("ALT is released") if (keyEvent.code == KeyCodes.SHIFT) println("SHIFT is released") if (keyEvent.code == KeyCodes.CTRL) println("CTRL is released") } println() } } while(keyEvent != null) sleep(100) } }
Read API documentation to find more details about Keyboard, Key and KeyCodes classes.