Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

A lightweight multiplatform library for interacting with global keyboard and mouse events and states from Kotlin, Java and NodeJS.

License

Notifications You must be signed in to change notification settings

Animeshz/keyboard-mouse-kt

Repository files navigation

KeyboardMouse.kt

Latest Release Bintray Version Code Size License

A lightweight multiplatform kotlin library for interacting with global keyboard and mouse events.

KeyboardMouse.kt is still in an experimental stage, as such we can't guarantee API stability between releases. While we'd love for you to try out our library, we don't recommend you use this in production just yet.

What is KeyboardMouse.kt

KeyboardMouse.kt is a lightweight, coroutine-based multiplatform kotlin library for idiomatically interacting with Keyboard and Mouse (receiving and sending global events).

We aim to provide high-level as well as high-performant low-level access to such APIs. See the usage section below to know more!

Status of KeyboardMouse.kt

  • Keyboard
    • Windows
      • x86_64 (64 bit)
      • x86 (32 bit)
    • Linux
      • x86_64 (64 bit)
      • x86 (32 bit)
    • MacOS
    • JVM
      • Windows x86_64 (64 bit)
      • Windows x86 (32 bit)
      • Linux x86_64 (64 bit)
      • Linux x86 (32 bit)
  • Mouse
    • Windows
    • Linux
    • MacOS
    • JVM

Installation

To add the library to your project, add the following the repository and dependency (build.gradle.kts):

repositories {
    maven(url = "https://dl.bintray.com/animeshz/maven")
}

kotlin {
    // Your targets
    jvm()
    mingwX64 {
        binaries { executable { entryPoint = "main" } }
    }
    linuxX64 {
        binaries { executable { entryPoint = "main" } }
    }

    // Dependency to the library
    sourceSets {
        // Either as common
        val commonMain by getting {
            dependencies {
                implementation(kotlin("stdlib-common"))
                implementation("com.github.animeshz:keyboard-kt:<version>")
                implementation("com.github.animeshz:mouse-kt:<version>")
            }
        }
        
        // Or configure each-platform by the suffix
        val jvmMain by getting {
            dependsOn(commonMain)
            dependencies {
                implementation("com.github.animeshz:keyboard-kt-jvm:<version>")
                implementation("com.github.animeshz:mouse-kt-jvm:<version>")
            }
        }
    }
}

Usage

Low Level API:

Low Level API depends on NativeKeyboardHandler that can be obtained via nativeKbHandlerForPlatform.

  • Listening to events using Flow.
    handler.events
        .filter { it.state == KeyState.KeyDown }
        .map { it.key }
        .collect { println(it) }
  • Sending a Key event.
    handler.sendEvent(KeyEvent(Key.A, KeyState.KeyDown))
  • Get KeyState (KeyDown or KeyUp) of the Key.
    handler.getKeyState(Key.A)
    handler.getKeyState(Key.RightAlt)
  • Get States of Toggleable Keys (returns a Boolean).
    handler.isCapsLockOn()
    handler.isNumLockOn()
    handler.isScrollLockOn()

High level API:

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

  • Adding a shortcut (Hotkey).
    keyboard.addShortcut(Key.LeftCtrl + Key.E, trigger = KeyState.KeyDown) {
        println("triggered")
    }
  • Send a KeySet to the host machine.
    keyboard.send(Key.LeftAlt + Key.M)
  • Write a sentence (String) on the host machine.
    keyboard.write("Hello Keyboard!")
  • Suspensive wait till a KeySet is pressed.
    keyboard.awaitTill(Key.LeftCtrl + Key.LeftShift + Key.R)
  • Record Key presses till specific KeySet is pressed into a KeyPressSequence.
    val records: KeyPressSequence = keyboard.recordTill(Key.LeftAlt + Key.A)
  • Play a recorded or created collection of Keys at defined order.
    keyboard.play(records, speedFactor = 1.25)

Contributing and future plans

The Github dicussions are open! Be sure to show your existence, say hi! and share if you have any upcoming ideas :)

Issues and PRs are always welcome!

For future plans and contributing to the project please checkout CONTRIBUTING.md