-
Notifications
You must be signed in to change notification settings - Fork 1
/
keyboard.go
65 lines (58 loc) · 1.49 KB
/
keyboard.go
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
/*
#cgo LDFLAGS: -lX11
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/XKBlib.h>
#include <stdio.h>
#include <ctype.h>
Bool running;
extern void keyboardEvent(char*,int);
static void startKeyboardListener() {
Display *display = XOpenDisplay(NULL);
XGrabKeyboard(display, DefaultRootWindow(display), True, GrabModeAsync, GrabModeAsync, CurrentTime);
XEvent event;
running = True;
while(running) {
XNextEvent(display, &event);
switch (event.type){
case KeyPress: {
int a;
char *key = XKeysymToString(*XGetKeyboardMapping(display,event.xkey.keycode,1,&a));
keyboardEvent(key,1);
continue;
}
case KeyRelease: {
int a;
char *key = XKeysymToString(*XGetKeyboardMapping(display,event.xkey.keycode,1,&a));
keyboardEvent(key,0);
continue;
}
}
}
XUngrabKeyboard(display, CurrentTime);
XFlush(display);
}
static void releaseKeyboard() {
running = False;
}
*/
import "C"
var keyboardCallback func(string, bool)
func startKeyboardListen(calback func(string, bool)) {
keyboardCallback = calback
go C.startKeyboardListener()
}
//export keyboardEvent
func keyboardEvent(key *C.char, pressed C.int) {
sKey := C.GoString(key)
press := false
if pressed == 1 {
press = true
}
keyboardCallback(sKey, press)
}
func releaseKeyboard() {
C.releaseKeyboard()
}