google-chrome-extensiongokeyboard-hook

Global Keyboard Input Go


Does Go provide a way to listen to global keyboard inputs? Essentially like a keylogger (however that's not my purpose) where it captures input if the focus is outside its own context.

Ideally it would be cross platform, but if not Windows only would work as well.

My end goal is a tool that is cross platform, doesn't require a runtime, that allows you to register global keyboard shortcuts to in app javascript invocations (mainly control Google Music). It'll be done by having a Chrome extension connect to a socket.io connection in the Golang app, and having the app then feed commands to the extension.


Solution

  • Capturing keyboard input outside the context of the application is platform specific. Go specifically does not have bindings to these APIs.

    Mac OS X has an Objective-C API called event taps. Windows has C++ function RegisterHotKey, although I am less familiar with Windows APIs.

    There may be toolkits / frameworks that allow you to do this cross-platform, but there currently aren't any in Go. If you find any in C, you may be able to hook it into Go using cgo.

    Perhaps you are over-complicating things? There is a page here that describes adding keyboard shortcuts for a Chrome extension.