Keyboard macros: Difference between revisions

From Rosetta Code
Content added Content deleted
m (Move omit to bottom (is this the norm?))
(C)
Line 27: Line 27:
}</lang>
}</lang>
See [http://www.autohotkey.com/forum/topic44290.html&highlight=vim ahk-viper-mode] for a context sensitive vi key bindings example.
See [http://www.autohotkey.com/forum/topic44290.html&highlight=vim ahk-viper-mode] for a context sensitive vi key bindings example.

=={{header|C}}==
{{libheader|Xlib}}

The following example grabs Alt+F6 and Alt+F7 system-wide on a X server.
<lang c>#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>

int main()
{
Display *d;
XEvent event;
d = XOpenDisplay(NULL);
if ( d != NULL ) {
/* or simply XK_F7 should work too */
XGrabKey(d, XKeysymToKeycode(d, XStringToKeysym("F7")),
Mod1Mask, /* normally it's Alt */
DefaultRootWindow(d), True, GrabModeAsync, GrabModeAsync);
XGrabKey(d, XKeysymToKeycode(d, XStringToKeysym("F6")),
Mod1Mask,
DefaultRootWindow(d), True, GrabModeAsync, GrabModeAsync);

for(;;)
{
XNextEvent(d, &event);
if ( event.type == KeyPress ) {
KeySym s = XLookupKeysym(&event.xkey, 0);
if ( s == XK_F7 ) {
printf("something's happened\n");
} else if ( s == XK_F6 ) {
break;
}
}
}

XUngrabKey(d, XKeysymToKeycode(d, XStringToKeysym("F7")), Mod1Mask, DefaultRootWindow(d));
XUngrabKey(d, XKeysymToKeycode(d, XStringToKeysym("F6")), Mod1Mask, DefaultRootWindow(d));
}
return EXIT_SUCCESS;
}</lang>


=={{header|Tcl}}==
=={{header|Tcl}}==

Revision as of 18:51, 27 September 2009

Task
Keyboard macros
You are encouraged to solve this task according to the task description, using any language you may know.

Show how to link user defined methods to user defined keys. An example of this is the facility provided by emacs for key bindings. These key bindings may be application-specific or system-wide; state which you have done.

AutoHotkey

<lang AutoHotkey>Loop, 200  ; loop 200 times while not paused {

 TrayTip, counting, %A_Index% press alt-p to pause
 Sleep, 1000

}

!p::  ; links alt-p key combination to the method pauseme() (system wide)

 pauseMe()

Return

!r::  ; links alt-r key combination to the method resume() (system wide)

 resume()

Return

pauseMe() {

 MsgBox, pausing`, press alt-r to resume
 Pause

}

resume() {

 TrayTip, resume, resuming, 2
 Pause, off

}</lang> See ahk-viper-mode for a context sensitive vi key bindings example.

C

Library: Xlib

The following example grabs Alt+F6 and Alt+F7 system-wide on a X server. <lang c>#include <stdio.h>

  1. include <stdlib.h>
  2. include <X11/Xlib.h>
  3. include <X11/keysym.h>

int main() {

 Display *d;
 XEvent event;
 
 d = XOpenDisplay(NULL);
 if ( d != NULL ) {
               /* or simply XK_F7 should work too */
   XGrabKey(d, XKeysymToKeycode(d, XStringToKeysym("F7")), 

Mod1Mask, /* normally it's Alt */ DefaultRootWindow(d), True, GrabModeAsync, GrabModeAsync);

   XGrabKey(d, XKeysymToKeycode(d, XStringToKeysym("F6")), 

Mod1Mask, DefaultRootWindow(d), True, GrabModeAsync, GrabModeAsync);

   for(;;)
   {
     XNextEvent(d, &event);
     if ( event.type == KeyPress ) {

KeySym s = XLookupKeysym(&event.xkey, 0); if ( s == XK_F7 ) { printf("something's happened\n"); } else if ( s == XK_F6 ) { break; }

     }
   }
   XUngrabKey(d, XKeysymToKeycode(d, XStringToKeysym("F7")), Mod1Mask, DefaultRootWindow(d));
   XUngrabKey(d, XKeysymToKeycode(d, XStringToKeysym("F6")), Mod1Mask, DefaultRootWindow(d));
 }
 return EXIT_SUCCESS;

}</lang>

Tcl

Library: Tk

All Tk bindings are bound to a context that is no wider than a particular application and is frequently smaller (e.g., a single dialog box or an individual widget). <lang tcl>package require Tk

  1. Show off some emacs-like bindings...

pack [label .l -text "C-x C-s to save, C-x C-c to quit"] focus . bind . <Control-x><Control-s> {

   tk_messageBox -message "We would save here"

} bind . <Control-x><Control-c> {exit}</lang>

Key-to-key mapping macros

A more direct macro-like facility (substituting one key sequence for another) would be: <lang tcl>bind . <F1> {

   foreach c [split "Macro demo!" {}] {
       event generate %W $c
   }

}</lang> This can then be wrapped up like this: <lang tcl>package require Tk proc sendMacro {w string} {

   foreach c [split $string {}] {
       # Will not work for “<” character...
       event generate $w $c
   }

} proc macro {key translation} {

   bind . <$key> [list sendMacro %W [string map {% %%} $translation]]

}

  1. Some demonstration macros

macro F1 "Help me!" macro F2 "You pressed the F2 key" macro F3 "I'm getting bored here..." pack [text .t]; # A place for you to test the macros</lang>

Vedit macro language

<lang vedit>// Configure a key to access menu item. // The menu item may then contain the commands directly, or it may call a macro from disk. // This has the advantage that the key binding is shown in the menu.

Key_Add("Shft-F6","[MENU]TV", OK)

// Configure a key to perform visual mode edit operations (similar to recorded key macro):

Key_Add("Numpad.Enter", "[CURSOR LEFT][LINE END][RETURN]", OK)

// Configure a key to execute macro language commands:

Key_Add("Numpad+", '[VISUAL EXIT] if(BB!=-1) { RCB(10,BB,BE) BB(-1) S("|@(10)",SET+CONFIRM) } else { S("",CONFIRM) }', OK)

// Configure a key to call a macro from disk. The old key assignment is not removed.

Key_Add("Ctrl-Shft-N",'[VISUAL EXIT] Call_File(100,"aspell.vdm","NEXT_ERROR")',INSERT+OK)

// Configure a two-key sequence (C-x C-c to save file).

Key_Add("^X C","[MENU]FS", OK)

// Remove a key assignment. If INSERT option was used when the key was assigned, the old assignment will come in effect again.

Key_Delete("Ctrl-Shft-N")</lang>