Keyboard macros: Difference between revisions

Added Go
(Added Go)
Line 164:
(meta-key "H" "(begin (writeln 'HELLO) (date 'today))") ; function call
</lang>
 
=={{header|Go}}==
{{libheader|Xlib}}
{{trans|C}}
<br>
Note that 'cgo' does not support C unions as such - it expresses them as byte arrays. Consequently, the easiest way to access a field of a union (such as XEvent) is to write a C assessor function for it and then invoke that function from the Go side.
 
Note also that if you pass 'nil' to the XOpenDisplay function, it defaults to the value of the DISPLAY environment variable which has to be in a certain format to enable a connection to the X server to be established - check [https://www.x.org/releases/X11R7.7/doc/libX11/libX11/libX11.html the documentation] for details.
<lang go>package main
 
/*
#cgo LDFLAGS: -lX11
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>
 
static inline Window DefaultRootWindow_macro(Display *dpy) {
return ScreenOfDisplay(dpy, DefaultScreen(dpy))->root;
}
 
static inline int getXEvent_type(XEvent event) {
return event.type;
}
 
static inline XKeyEvent getXEvent_xkey(XEvent event) {
return event.xkey;
}
*/
import "C"
import "fmt"
import "unsafe"
 
func main() {
d := C.XOpenDisplay(nil)
f7, f6 := C.CString("F7"), C.CString("F6")
defer C.free(unsafe.Pointer(f7))
defer C.free(unsafe.Pointer(f6))
 
if d != nil {
C.XGrabKey(d, C.int(C.XKeysymToKeycode(d, C.XStringToKeysym(f7))),
C.Mod1Mask, /* normally it's Alt */
C.DefaultRootWindow_macro(d), C.True, C.GrabModeAsync, C.GrabModeAsync)
C.XGrabKey(d, C.int(C.XKeysymToKeycode(d, C.XStringToKeysym(f6))),
C.Mod1Mask,
C.DefaultRootWindow_macro(d), C.True, C.GrabModeAsync, C.GrabModeAsync)
 
var event C.XEvent
for {
C.XNextEvent(d, &event)
if C.getXEvent_type(event) == C.KeyPress {
xkeyEvent := C.getXEvent_xkey(event)
s := C.XLookupKeysym(&xkeyEvent, 0)
if s == C.XK_F7 {
fmt.Println("something's happened")
} else if s == C.XK_F6 {
break
}
}
}
 
C.XUngrabKey(d, C.int(C.XKeysymToKeycode(d, C.XStringToKeysym(f7))), C.Mod1Mask, C.DefaultRootWindow_macro(d))
C.XUngrabKey(d, C.int(C.XKeysymToKeycode(d, C.XStringToKeysym(f6))), C.Mod1Mask, C.DefaultRootWindow_macro(d))
} else {
fmt.Println("XOpenDisplay did not succeed")
}
}</lang>
 
=={{header|HicEst}}==
9,476

edits