Skip to main content

Keyboard

Plug a USB keyboard into the Pi, and your program can read it one key at a time.

Add the keyboard library first

Keyboard comes from a library, not from the language, so this code won't build without it. In IntelliJ, choose Tools | Nylon | Add Nylon Library… and pick keyboard. That adds one line to your project's build.markup.code:

(+dependencies : RepositoryDependency coordinate={"com.grandtheorem:keyboard:0.1.0"} /)

Read what's typed​

Keyboard comes from the keyboard library, and nylon.time gives us sleep.

# Print each key typed on a USB keyboard.
namespace typing

import grandtheorem.keyboard.Keyboard
import namespace nylon.time

Keyboard.resolve(0) gives you the first keyboard plugged in. Until there is one it gives nothing, so the program asks again each time round.

# Look for a keyboard each time round,
# so one plugged in later is found too.
entrypoint.default main() {
while (true) {
if (let keyboard? = Keyboard.resolve(0)) {

readKey waits for the next key, up to the timeout. If none comes, there's no key, and the if skips its block.

# Wait up to a second for a key.
# If none comes, go round again.
if (let key? = keyboard.readKey(1.seconds())) {

character is the text the key types. Keys like the arrows type nothing, so it may be missing, but every key has a code. With no keyboard plugged in, wait a moment before looking again.

# Letters, digits and the like have a character.
# Others, such as the arrows, only a code.
if (let character? = key.character) {
println(character)
} else {
println("key ${key.code} types nothing")
}
}
} else {
# No keyboard yet: look again in a moment.
sleep(100.milliseconds())
}
}
}

Each key you press shows up in the run window. The program loops forever, so press Stop in IntelliJ to end it.

React to a key​

For keys that don't type anything, use code. These are the common ones:

KeyCode
Right arrow79
Left arrow80
Down arrow81
Up arrow82
Enter40
Escape41
Space44

Name the codes you care about, so the code reads as what it means.

# Move a position left and right with the arrow keys.
# 80 and 79 are the left and right arrows' key codes.
namespace arrows

import grandtheorem.keyboard.Keyboard
import namespace nylon.time

let LEFT = 80
let RIGHT = 79

position changes as the program runs, so it's a var.

# var, since the position changes as keys come in.
entrypoint.default main() {
var position = 0

Compare each key's code. Any other key leaves position alone.

# Step left or right, then say where it is now.
while (true) {
if (let keyboard? = Keyboard.resolve(0)) {
if (let key? = keyboard.readKey(1.seconds())) {
if (key.code == LEFT) {
position = position - 1
} elif (key.code == RIGHT) {
position = position + 1
}

println("position ${position}")
}
} else {
sleep(100.milliseconds())
}
}
}

Good to know​

A held key counts once. Holding a key down doesn't repeat it, and there's no way yet to tell when it's released.

Letters follow Shift, not Caps Lock. A letter is a capital only while Shift is held. character assumes a US layout.

Enter types "\n". If you're sending text to something that expects \r\n, swap it yourself.

No keyboard is not an error. Keyboard.resolve(0) finds nothing until one is plugged in.

Up to four keyboards. resolve takes 0 to 3, one for each keyboard plugged in. Any other number ends the run with a fault that names it.

USB only. Bluetooth support, so a keyboard can pair without a cable, comes in V2.

Boards: Raspberry Pi 4. Zero 2 W: soon.