├── .gitignore ├── LICENSE ├── README.md ├── ex └── stdout │ └── main.go ├── keymap.go └── xkg.go /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | *.test 24 | *.prof 25 | 26 | xkg 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Henrique Menezes 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | xkg - X Keyboard Grabber 2 | ======================== 3 | 4 | [![GoDoc](https://godoc.org/github.com/go-xkg/xkg?status.png)](https://godoc.org/github.com/go-xkg/xkg) 5 | 6 | ## Installation 7 | 8 | go get gopkg.in/xkg.v0 9 | 10 | ## Usage example: 11 | 12 | ```go 13 | package main 14 | 15 | import ( 16 | "fmt" 17 | "gopkg.in/xkg.v0" 18 | ) 19 | 20 | func main() { 21 | var keys = make(chan int, 100) 22 | 23 | go xkg.StartXGrabber(keys) 24 | 25 | for { 26 | keycode := <-keys 27 | 28 | if key, ok := xkg.KeyMap[keycode]; ok { 29 | fmt.Printf("[%s]", key) 30 | } 31 | } 32 | } 33 | ``` 34 | 35 | ## License 36 | 37 | The license of the project is [The MIT License (MIT)](https://github.com/henriquemenezes/xkg/blob/master/LICENSE). 38 | -------------------------------------------------------------------------------- /ex/stdout/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "gopkg.in/xkg.v0" 7 | ) 8 | 9 | func main() { 10 | var keys = make(chan int, 100) 11 | 12 | go xkg.StartXGrabber(keys) 13 | 14 | for { 15 | keycode := <-keys 16 | 17 | if key, ok := xkg.KeyMap[keycode]; ok { 18 | fmt.Printf("[%s]", key) 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /keymap.go: -------------------------------------------------------------------------------- 1 | package xkg 2 | 3 | var KeyMap map[int]string = map[int]string{ 4 | // Modifier keys 5 | 50: "", 6 | 62: "", 7 | 37: "", 8 | 105: "", 9 | 64: "alt", 10 | 108: "alt", 11 | 133: "133", 12 | 13 | // Qwerty keys 14 | // qwerty row 15 | 24: "q", 16 | 25: "w", 17 | 26: "e", 18 | 27: "r", 19 | 28: "t", 20 | 29: "y", 21 | 30: "u", 22 | 31: "i", 23 | 32: "o", 24 | 33: "p", 25 | 34: "[", 26 | 35: "]", 27 | 51: "\\", 28 | 29 | // asdf row 30 | 38: "a", 31 | 39: "s", 32 | 40: "d", 33 | 41: "f", 34 | 42: "g", 35 | 43: "h", 36 | 44: "j", 37 | 45: "k", 38 | 46: "l", 39 | 47: ",", 40 | 48: "\"", 41 | 36: "", 42 | 43 | // zxcv row 44 | 52: "z", 45 | 53: "x", 46 | 54: "c", 47 | 55: "v", 48 | 56: "b", 49 | 57: "n", 50 | 58: "m", 51 | 59: ",", 52 | 60: ".", 53 | 61: "/", 54 | 55 | // number row 56 | 49: "`", 57 | 10: "1", 58 | 11: "2", 59 | 12: "3", 60 | 13: "4", 61 | 14: "5", 62 | 15: "6", 63 | 16: "7", 64 | 17: "8", 65 | 18: "9", 66 | 19: "0", 67 | 20: "-", 68 | 21: "+", 69 | 70 | // space bar 71 | 65: " ", 72 | 73 | // number pad 74 | 90: "", 75 | 87: "", 76 | 88: "", 77 | 89: "", 78 | 83: "", 79 | 84: "", 80 | 85: "", 81 | 79: "", 82 | 80: "", 83 | 81: "", 84 | 106: "", 85 | 63: "", 86 | 82: "", 87 | 86: "", 88 | 89 | // F keys 90 | 67: "", 91 | 68: "", 92 | 69: "", 93 | 70: "", 94 | 71: "", 95 | 72: "", 96 | 73: "", 97 | 74: "", 98 | 75: "", 99 | 76: "", 100 | 95: "", 101 | 96: "", 102 | 103 | // Misc 104 | 9: "", 105 | 22: "", 106 | 77: "", 107 | 107: "", 108 | 118: "", 109 | 119: "", 110 | 110: "", 111 | 112: "", 112 | 117: "", 113 | 115: "", 114 | 111: "", 115 | 116: "", 116 | 113: "", 117 | 114: "", 118 | 135: "", 119 | 23: "", 120 | 66: "", 121 | } 122 | -------------------------------------------------------------------------------- /xkg.go: -------------------------------------------------------------------------------- 1 | package xkg 2 | 3 | // #cgo pkg-config: x11 xext xi 4 | // 5 | // #include 6 | // #include 7 | // #include 8 | // #include 9 | // #include 10 | // #include 11 | // 12 | // int defaultScreen(Display *display) { 13 | // return DefaultScreen(display); 14 | // } 15 | // 16 | // Window rootWindow(Display *display, int screen) { 17 | // return RootWindow(display, screen); 18 | // } 19 | // 20 | // void deviceKeyPress(XDevice *device, int *type, XEventClass *event) { 21 | // DeviceKeyPress(device, *type, *event); 22 | // } 23 | // 24 | // void deviceKeyRelease(XDevice *device, int *type, XEventClass *event) { 25 | // DeviceKeyRelease(device, *type, *event); 26 | // } 27 | // 28 | // int isType(XEvent *event, int type) { 29 | // return event->type == type; 30 | // } 31 | import "C" 32 | 33 | import ( 34 | "os" 35 | "reflect" 36 | "unsafe" 37 | ) 38 | 39 | // Constants 40 | const KeyClass = 0 41 | const InvalidType C.int = -1 42 | 43 | // Global Variables 44 | var cKeyPressType C.int = InvalidType 45 | var cKeyReleaseType C.int = InvalidType 46 | 47 | // StartXGrabber starts the X Keyboard Grabber 48 | // keys - channel to send keycodes 49 | func StartXGrabber(keys chan int) { 50 | var cDisplay *C.Display 51 | var cDevice *C.XDeviceInfo 52 | var cNumEvents C.int 53 | 54 | // Open X Display 55 | cDisplay = C.XOpenDisplay(nil) 56 | 57 | // Get Keyboard Device 58 | cDevice = findKeyboardDevice(cDisplay) 59 | 60 | // Unable to find device 61 | if cDevice == nil { 62 | os.Exit(1) 63 | } 64 | 65 | // Register Events 66 | cNumEvents = registerEvents(cDisplay, cDevice) 67 | 68 | // No events registered 69 | if cNumEvents == 0 { 70 | os.Exit(1) 71 | } 72 | 73 | // Grab X keyboard events 74 | grabXEvents(cDisplay, keys) 75 | } 76 | 77 | // grabXEvents captures the keyboard events of Display X 78 | func grabXEvents(cDisplay *C.Display, keys chan int) { 79 | var cEvent C.XEvent 80 | var cKey *C.XDeviceKeyEvent 81 | 82 | for { 83 | C.XNextEvent(cDisplay, &cEvent) 84 | keyPressed := (C.isType(&cEvent, cKeyPressType) != 0) 85 | 86 | if keyPressed { 87 | // Convert C.XEvent into *C.XDeviceKeyEvent 88 | cKey = ((*C.XDeviceKeyEvent)(unsafe.Pointer(&cEvent))) 89 | 90 | // Send Keycode to channel 91 | keys <- int(cKey.keycode) 92 | } else { 93 | // unknown event 94 | } 95 | } 96 | } 97 | 98 | // findKeyboardDevice return the default keyboard device (AT Translated Set 2 keyboard) 99 | func findKeyboardDevice(cDisplay *C.Display) *C.XDeviceInfo { 100 | var cDevices *C.XDeviceInfo 101 | var cFound *C.XDeviceInfo 102 | var cNumDevices C.int 103 | 104 | cDevices = C.XListInputDevices(cDisplay, &cNumDevices) 105 | devices := toXDeviceInfoSlice(cDevices, cNumDevices) 106 | 107 | for _, device := range devices { 108 | if C.strcmp(device.name, C.CString("AT Translated Set 2 keyboard")) == 0 { 109 | cFound = &device 110 | break 111 | } 112 | } 113 | 114 | return cFound 115 | } 116 | 117 | // registerEvents register KeyPress and KeyRelease event classes into keyboard device 118 | func registerEvents(cDisplay *C.Display, cDeviceInfo *C.XDeviceInfo) C.int { 119 | var cNumEvents C.int 120 | var cEventList [2]C.XEventClass 121 | var cEventListPtr *C.XEventClass 122 | var cDevice *C.XDevice 123 | var cRootWin C.Window 124 | var cScreen C.int 125 | var classes []C.XInputClassInfo 126 | 127 | cScreen = C.defaultScreen(cDisplay) 128 | cRootWin = C.rootWindow(cDisplay, cScreen) 129 | cDevice = C.XOpenDevice(cDisplay, cDeviceInfo.id) 130 | 131 | if cDevice == nil { 132 | // unable to open device 133 | return cNumEvents 134 | } 135 | 136 | if cDevice.num_classes > 0 { 137 | classes = toXInputClassInfoSlice(cDevice.classes, cDevice.num_classes) 138 | 139 | for _, class := range classes { 140 | switch class.input_class { 141 | case KeyClass: 142 | C.deviceKeyPress(cDevice, &cKeyPressType, &cEventList[cNumEvents]) 143 | cNumEvents += 1 144 | C.deviceKeyRelease(cDevice, &cKeyReleaseType, &cEventList[cNumEvents]) 145 | cNumEvents += 1 146 | default: 147 | // unknown class 148 | } 149 | } 150 | 151 | // Convert [2]C.XEventClass into *C.XEventClass 152 | cEventListPtr = ((*C.XEventClass)(unsafe.Pointer(&cEventList))) 153 | 154 | if C.XSelectExtensionEvent(cDisplay, cRootWin, cEventListPtr, cNumEvents) != 0 { 155 | // error selecting extended events 156 | return 0 157 | } 158 | } 159 | 160 | return cNumEvents 161 | } 162 | 163 | // toXDeviceInfoSlice converts *C.XDeviceInfo into []C.XDeviceInfo 164 | func toXDeviceInfoSlice(array *C.XDeviceInfo, length C.int) []C.XDeviceInfo { 165 | // Convert C arrays into Go slices 166 | // Ref: https://code.google.com/p/go-wiki/wiki/cgo 167 | hdr := reflect.SliceHeader{ 168 | Data: uintptr(unsafe.Pointer(array)), 169 | Len: int(length), 170 | Cap: int(length), 171 | } 172 | 173 | return *(*[]C.XDeviceInfo)(unsafe.Pointer(&hdr)) 174 | } 175 | 176 | // toXInputClassInfoSlice converts *C.XInputClassInfo into []C.XInputClassInfo 177 | func toXInputClassInfoSlice(array *C.XInputClassInfo, length C.int) []C.XInputClassInfo { 178 | // Convert C arrays into Go slices 179 | // Ref: https://code.google.com/p/go-wiki/wiki/cgo 180 | hdr := reflect.SliceHeader{ 181 | Data: uintptr(unsafe.Pointer(array)), 182 | Len: int(length), 183 | Cap: int(length), 184 | } 185 | 186 | return *(*[]C.XInputClassInfo)(unsafe.Pointer(&hdr)) 187 | } 188 | --------------------------------------------------------------------------------