I made a Go program to simulate key presses. To do that I had to use cgo and different snippet of C code depending on the OS the Go code was being compiled. The code that I wrote looks like this:
package keyboard
/*
#include <stdint.h>
#ifdef __WIN32
#cgo CFLAGS:-nostdlib
#include <Windows.h>
void SetKey(uint16_t key, uint8_t value) {
INPUT ip;
ip.type = INPUT_KEYBOARD;
ip.ki.wScan = 0;
ip.ki.time = 0;
ip.ki.dwExtraInfo = 0;
ip.ki.wVk = key;
if (value) {
ip.ki.dwFlags = 0;
} else {
ip.ki.dwFlags = KEYEVENTF_KEYUP;
}
SendInput(1, &ip, sizeof(INPUT));
}
#endif
#ifdef __linux__
#cgo LDFLAGS: -lX11 -lXtst
#include <stdlib.h>
#include <stdio.h> //TODO: REMOVE
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/extensions/XTest.h>
void SetKey(uint16_t key, uint8_t value) {
Display *display;
display = XOpenDisplay(NULL);
if(display == NULL) {
exit(EXIT_FAILURE);
}
XTestFakeKeyEvent(display,XKeysymToKeycode(display,key), value, 0);
XCloseDisplay(display);
}
#endif
*/
import "C"
func SetKey(keyId uint16, value bool) {
C.SetKey(C.uint16_t(keyId),boolToByte(value));
}
func boolToByte(value bool) C.uint8_t {
if(value) {
return 1
} else {
return 0
}
}
The code compiles fine on Ubuntu, but on Windows 10 I get the following error
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/5.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lX11
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/5.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lXtst
even if the line #cgo LDFLAGS: -lX11 -lXtst
is wrapped around #ifdef __linux__
and #endif
.
Where does the problem lie on?
Does the GCC compiler define the __linux__
macro?
Is the keyword #cgo
not supposed to be used like that?