Skip to content

High Level API

Kotlin: High Level API depends on Keyboard which is a wrapper around the NativeKeyboardHandler.

Java: High Level API depends on JKeyboard.

NodeJS: High Level API depends on JsKeyboard.

Importing the package.

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

val keyboard = Keyboard()

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

const keyboard = new kbkt.com.github.animeshz.keyboard.JsKeyboard();
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.JKeyboard;

JKeyboard handler = new JKeyboard();

Adding a shortcut (Hotkey).

1
2
3
keyboard.addShortcut(Key.LeftCtrl + Key.E, trigger = KeyState.KeyDown) {
    println("triggered")
}
Note: The lambda is in suspend context, launched in context provided at time of instantiation of Keyboard (defaults to Dispatchers.Default).

1
2
3
keyboard.addShortcut('LeftCtrl + E', true,
    () => console.log("triggered")
);
1
2
3
4
5
6
Set<Key> keys = new HashSet<>();
Collections.addAll(keys, Key.LeftCtrl, Key.E);

keyboard.addShortcut(new KeySet(keys), KeyState.KeyDown,
    () -> System.out.println("triggered")
);
1
2
3
4
5
Set<Key> keys = Set.of(Key.LeftCtrl, Key.E);

keyboard.addShortcut(new KeySet(keys), KeyState.KeyDown,
    () -> System.out.println("triggered")
);

Note: trigger defaults to KeyState.KeyDown when not provided.

Send a KeySet to the host machine.

1
keyboard.send(Key.LeftAlt + Key.M)
1
keyboard.send('LeftAlt + M');
1
2
3
4
Set<Key> keys = new HashSet<>();
Collections.addAll(keys, Key.LeftAlt, Key.M);

keyboard.send(new KeySet(keys));
1
2
3
Set<Key> keys = Set.of(Key.LeftAlt, Key.M);

keyboard.send(new KeySet(keys));

Write a sentence (String) on the host machine.

1
keyboard.write("Hello Keyboard!")
1
keyboard.write('Hello Keyboard!');
1
keyboard.write("Hello Keyboard!");

Wait till a KeySet is pressed.

Suspensive wait in Kotlin, whereas asynchronous CompletableFuture<> for Java

1
keyboard.awaitTill(Key.LeftCtrl + Key.LeftShift + Key.R, trigger = KeyState.KeyDown)
1
await keyboard.completeWhenPressed('LeftCtrl + LeftShift + R');

1
2
3
4
5
Set<Key> keys = new HashSet<>();
Collections.addAll(keys, Key.LeftCtrl + Key.LeftShift + Key.R);

keyboard.completeWhenPressed(new KeySet(keys), KeyState.KeyDown)
    .thenApply(unit -> {...});
Note: Unit is similar to java.lang.Void, a singleton object which has nothing to do for us.

1
2
3
4
Set<Key> keys = Set.of(Key.LeftCtrl + Key.LeftShift + Key.R);

keyboard.completeWhenPressed(new KeySet(keys), KeyState.KeyDown)
    .thenApply(unit -> {...});
Note: Unit is similar to java.lang.Void, a singleton object which has nothing to do for us.

Note: trigger defaults to KeyState.KeyDown when not provided.

Record Key presses till specific KeySet.

Recorded KeyPresses is pushed into a KeyPressSequence (List<Duration, KeyEvent>)

1
val records: KeyPressSequence = keyboard.recordTill(Key.LeftAlt + Key.A, trigger = KeyState.KeyDown)
1
const records = await keyboard.recordKeyPressesTill('LeftCtrl + LeftShift + R', true);
1
2
3
4
5
6
Set<Key> keys = new HashSet<>();
Collections.addAll(keys, Key.LeftAlt, Key.A);

// `trigger` defaults to KeyState.KeyDown when not provided.
CompletableFuture<List<Duration, KeyEvent>> records =
    keyboard.recordTill(new KeySet(keys), KeyState.KeyDown);
1
2
3
4
Set<Key> keys = Set.of(Key.LeftAlt, Key.A);

CompletableFuture<List<Duration, KeyEvent>> records =
    keyboard.recordTill(new KeySet(keys), KeyState.KeyDown);

Play a recorded or created collection of Keys at defined order.

1
keyboard.play(records, speedFactor = 1.25)
1
await keyboard.play(records, 1.0);
1
CompletableFuture<Unit> onFinish = keyboard.play(records, 1.25)

Note: speedFactor defaults to 1.0 when not provided.