Skip to content

Commit

Permalink
✨ Support customised shortcut key system (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
guiyanakuang authored Nov 27, 2023
1 parent bfe3b34 commit 25ee935
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions composeApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ kotlin {
implementation("org.signal:libsignal-client:0.35.0")
implementation("br.com.devsrsouza.compose.icons:tabler-icons-desktop:1.1.0")
implementation("io.insert-koin:koin-core:3.5.0")
implementation("com.github.kwhat:jnativehook:2.2.2")
}
commonMain.dependencies {
implementation(compose.runtime)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.clipevery.listen

import com.github.kwhat.jnativehook.GlobalScreen
import com.github.kwhat.jnativehook.NativeHookException

import com.github.kwhat.jnativehook.keyboard.NativeKeyEvent

import com.github.kwhat.jnativehook.keyboard.NativeKeyListener
import io.github.oshai.kotlinlogging.KotlinLogging

val logger = KotlinLogging.logger {}

class GlobalListener {

init {
try {
GlobalScreen.registerNativeHook()
} catch (ex: NativeHookException) {
logger.error { "There was a problem registering the native hook." }
logger.error { "ex.message" }
}
GlobalScreen.addNativeKeyListener(GlobalKeyListenerExample())
}

}


class GlobalKeyListenerExample : NativeKeyListener {
override fun nativeKeyPressed(e: NativeKeyEvent) {
logger.info { "Key Pressed: " + NativeKeyEvent.getKeyText(e.keyCode) }
if (e.keyCode == NativeKeyEvent.VC_ESCAPE) {
try {
GlobalScreen.unregisterNativeHook()
} catch (nativeHookException: NativeHookException) {
nativeHookException.printStackTrace()
}
}
}

override fun nativeKeyReleased(e: NativeKeyEvent) {
logger.info { "Key Released: " + NativeKeyEvent.getKeyText(e.keyCode) }
}

override fun nativeKeyTyped(e: NativeKeyEvent) {
logger.info { "Key Typed: " + NativeKeyEvent.getKeyText(e.keyCode) }
}
}
4 changes: 4 additions & 0 deletions composeApp/src/desktopMain/kotlin/com/clipevery/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.clipevery.encrypt.SignalProtocolWithState
import com.clipevery.encrypt.getSignalProtocolFactory
import com.clipevery.i18n.GlobalCopywriter
import com.clipevery.i18n.GlobalCopywriterImpl
import com.clipevery.listen.GlobalListener
import com.clipevery.log.initLogger
import com.clipevery.model.AppInfo
import com.clipevery.net.ClipServer
Expand Down Expand Up @@ -69,6 +70,7 @@ fun initKoinApplication(ioScope: CoroutineScope): KoinApplication {
single<GlobalCopywriter> { GlobalCopywriterImpl(get()) }
single<ClipboardService> { getDesktopClipboardService(get()) }
single<TransferableConsumer> { DesktopTransferableConsumer() }
single<GlobalListener> { GlobalListener() }
}
return startKoin {
modules(appModule)
Expand All @@ -88,6 +90,8 @@ fun main() = application {

val koinApplication by remember { mutableStateOf(initKoinApplication(ioScope)) }

koinApplication.koin.get<GlobalListener>()

val trayIcon = if(currentPlatform().isMacos()) {
painterResource("clipevery_mac_tray.png")
} else {
Expand Down

0 comments on commit 25ee935

Please sign in to comment.