API documentation

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)
    }
}

Read API documentation to find more details about Keyboard, Key and KeyCodes classes.