Skip to content

Low Level API

Kotlin: Low Level API depends on NativeKeyboardHandler that can be obtained via nativeKbHandlerForPlatform().

Java: Low Level API depends on JNativeKeyboardHandler that can be obtained via JNativeKeyboardHandler.INSTANCE.

NodeJS: Low Level API depends on JsKeyboardHandler that can be obtained via JsNativeKeyboardHandler.

Importing the package.

1
2
3
import com.github.animeshz.keyboard.nativeKbHandlerForPlatform

val handler = nativeKbHandlerForPlatform()

1
2
3
const kbkt = require('keyboard-kt');

const handler = kbkt.com.github.animeshz.keyboard.JsNativeKeyboardHandler;
Note: This large import is due to limitations of K/JS to not able to export to global namespace currently, see KT-37710.

1
2
3
import com.github.animeshz.keyboard.JKeyboardHandler;

JKeyboardHandler handler = JKeyboardHandler.INSTANCE;

Listening to events using Flow (Kotlin) or callback (Java).

1
2
3
4
handler.events
    .filter { it.state == KeyState.KeyDown }
    .map { it.key }
    .collect { println(it) }
1
2
3
4
5
handler.addHandler((key, pressed) => {
    if (pressed) {
        console.log(key);
    }
});
1
2
3
4
5
handler.addHandler(keyEvent -> {
    if (keyEvent.state == KeyState.KeyDown) {
        System.out.println(keyEvent.key);
    }
});

Sending a Key event.

1
handler.sendEvent(KeyEvent(Key.A, KeyState.KeyDown))
1
handler.sendEvent('A', true);
1
handler.sendEvent(new KeyEvent(Key.A, KeyState.KeyDown));

Get KeyState (KeyDown or KeyUp) of the Key.

1
2
handler.getKeyState(Key.A)
handler.getKeyState(Key.RightAlt)

1
2
handler.getKeyState('A');
handler.getKeyState('RightAlt');
Note: In JS it returns a boolean

1
2
handler.getKeyState(Key.A);
handler.getKeyState(Key.RightAlt);

Get States of Toggleable Keys (returns a Boolean).

1
2
3
handler.isCapsLockOn()
handler.isNumLockOn()
handler.isScrollLockOn()
1
2
3
handler.isCapsLockOn();
handler.isNumLockOn();
handler.isScrollLockOn();
1
2
3
handler.isCapsLockOn();
handler.isNumLockOn();
handler.isScrollLockOn();