Keyboard macros: Difference between revisions

Content added Content deleted
(Added Wren)
m (syntax highlighting fixup automation)
Line 8: Line 8:


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang AutoHotkey>Loop, 200 ; loop 200 times while not paused
<syntaxhighlight lang="autohotkey">Loop, 200 ; loop 200 times while not paused
{
{
TrayTip, counting, %A_Index% press alt-p to pause
TrayTip, counting, %A_Index% press alt-p to pause
Line 32: Line 32:
TrayTip, resume, resuming, 2
TrayTip, resume, resuming, 2
Pause, off
Pause, off
}</lang>
}</syntaxhighlight>
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|BBC BASIC}}==
=={{header|BBC BASIC}}==
===Native===
===Native===
<lang bbcbasic> *KEY 1 |!|A
<syntaxhighlight lang="bbcbasic"> *KEY 1 |!|A
*KEY 2 |!|B
*KEY 2 |!|B
REPEAT
REPEAT
Line 54: Line 54:
DEF PROCmethod2
DEF PROCmethod2
PRINT "You pressed F2"
PRINT "You pressed F2"
ENDPROC</lang>
ENDPROC</syntaxhighlight>
===Windows===
===Windows===
{{works with|BBC BASIC for Windows}}
{{works with|BBC BASIC for Windows}}
<lang bbcbasic> FVIRTKEY = 1
<syntaxhighlight lang="bbcbasic"> FVIRTKEY = 1
VK_F1 = &70
VK_F1 = &70
VK_F2 = &71
VK_F2 = &71
Line 89: Line 89:
DEF PROCmethod2
DEF PROCmethod2
PRINT "You pressed F2"
PRINT "You pressed F2"
ENDPROC</lang>
ENDPROC</syntaxhighlight>


=={{header|C}}==
=={{header|C}}==
Line 95: Line 95:


The following example grabs Alt+F6 and Alt+F7 system-wide on a X server.
The following example grabs Alt+F6 and Alt+F7 system-wide on a X server.
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xlib.h>
Line 132: Line 132:
}
}
return EXIT_SUCCESS;
return EXIT_SUCCESS;
}</lang>
}</syntaxhighlight>


=={{header|Clojure}}==
=={{header|Clojure}}==
Line 138: Line 138:


The following example is at application level, printing out what key was pressed:
The following example is at application level, printing out what key was pressed:
<lang clojure>
<syntaxhighlight lang="clojure">
(ns hello-seesaw.core
(ns hello-seesaw.core
(:use seesaw.core))
(:use seesaw.core))
Line 149: Line 149:
pack!
pack!
show!)))
show!)))
</syntaxhighlight>
</lang>


=={{header|Delphi}}==
=={{header|Delphi}}==
Version Console Application
Version Console Application
<syntaxhighlight lang="delphi">
<lang Delphi>
program Key_Bindings_test;
program Key_Bindings_test;


Line 276: Line 276:
readln;
readln;
end.
end.
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 289: Line 289:
=={{header|EchoLisp}}==
=={{header|EchoLisp}}==
The '''(meta-key "key-value" "bound-string")''' function binds a modifier+key keypress to a string or function call.
The '''(meta-key "key-value" "bound-string")''' function binds a modifier+key keypress to a string or function call.
<lang lisp>
<syntaxhighlight lang="lisp">
;; see initial bindings : GREEK DICTIONARY
;; see initial bindings : GREEK DICTIONARY
(meta-keys) → (("0" "❌") ("1" "❗️") ("2" "❓") ("3" "✔️") ("4" "⛔️") ("5" "✅") ("6" "🚩") ("7" "⌚️")
(meta-keys) → (("0" "❌") ("1" "❗️") ("2" "❓") ("3" "✔️") ("4" "⛔️") ("5" "✅") ("6" "🚩") ("7" "⌚️")
Line 306: Line 306:
(meta-key "A" "Antoinette") ; string
(meta-key "A" "Antoinette") ; string
(meta-key "H" "(begin (writeln 'HELLO) (date 'today))") ; function call
(meta-key "H" "(begin (writeln 'HELLO) (date 'today))") ; function call
</syntaxhighlight>
</lang>


=={{header|Go}}==
=={{header|Go}}==
Line 315: Line 315:


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.
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
<syntaxhighlight lang="go">package main


/*
/*
Line 372: Line 372:
fmt.Println("XOpenDisplay did not succeed")
fmt.Println("XOpenDisplay did not succeed")
}
}
}</lang>
}</syntaxhighlight>


=={{header|HicEst}}==
=={{header|HicEst}}==
<lang hicest>! bound to application
<syntaxhighlight lang="hicest">! bound to application
CALL F2
CALL F2
! ...
! ...
Line 396: Line 396:
! Fn is suspended if m > n AND n <= 5, else Fm is queued
! Fn is suspended if m > n AND n <= 5, else Fm is queued
! ...
! ...
END</lang>
END</syntaxhighlight>


=={{header|Icon}} and {{header|Unicon}}==
=={{header|Icon}} and {{header|Unicon}}==


This is application-specific and works in both languages:
This is application-specific and works in both languages:
<lang unicon>
<syntaxhighlight lang="unicon">
global kMap
global kMap


Line 422: Line 422:
procedure italicsOff()
procedure italicsOff()
return "<\\i>"
return "<\\i>"
end</lang>
end</syntaxhighlight>


Sample run:
Sample run:
Line 432: Line 432:


=={{header|Java}}==
=={{header|Java}}==
<lang java>
<syntaxhighlight lang="java">
package keybord.macro.demo;
package keybord.macro.demo;


Line 463: Line 463:
}
}
}
}
</syntaxhighlight>
</lang>


=={{header|JavaScript}}==
=={{header|JavaScript}}==
The example below captures the F7 key when pressed, if the document (that is, the web page) has focus. If the function returns ''false'', the event processing is halted. If it returns any other value, including ''undefined'', the event continues up the DOM tree ('bubbling').
The example below captures the F7 key when pressed, if the document (that is, the web page) has focus. If the function returns ''false'', the event processing is halted. If it returns any other value, including ''undefined'', the event continues up the DOM tree ('bubbling').


<lang javascript>document.onkeydown = function(evt) {
<syntaxhighlight lang="javascript">document.onkeydown = function(evt) {
if (evt.keyCode === 118) {
if (evt.keyCode === 118) {
alert("You pressed F7!");
alert("You pressed F7!");
return false;
return false;
}
}
}</lang>
}</syntaxhighlight>


See [http://www.quirksmode.org/js/keys.html quirksmode] for more information about key detection in JavaScript.
See [http://www.quirksmode.org/js/keys.html quirksmode] for more information about key detection in JavaScript.
Line 479: Line 479:
=={{header|Julia}}==
=={{header|Julia}}==
Macros are within the Gtk window.
Macros are within the Gtk window.
<lang julia>using Gtk
<syntaxhighlight lang="julia">using Gtk


function keypresswindow()
function keypresswindow()
Line 501: Line 501:


keypresswindow()
keypresswindow()
</syntaxhighlight>
</lang>


=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|Java}}
{{trans|Java}}
<lang scala>// version 1.2.31
<syntaxhighlight lang="scala">// version 1.2.31


import javax.swing.JFrame
import javax.swing.JFrame
Line 529: Line 529:
isVisible = true
isVisible = true
}
}
}</lang>
}</syntaxhighlight>


=={{header|M2000 Interpreter}}==
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module CheckIt {
Module CheckIt {
\\ Scan statement exist from version 1
\\ Scan statement exist from version 1
Line 564: Line 564:
}
}
Checkit
Checkit
</syntaxhighlight>
</lang>


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Map the keystroke t to a simple script
Map the keystroke t to a simple script
<lang Mathematica>SetOptions[EvaluationNotebook[], NotebookEventActions -> {{"KeyDown", "t"} :> Print["You pressed \"t\""]}]</lang>
<syntaxhighlight lang="mathematica">SetOptions[EvaluationNotebook[], NotebookEventActions -> {{"KeyDown", "t"} :> Print["You pressed \"t\""]}]</syntaxhighlight>


=={{header|Nim}}==
=={{header|Nim}}==
{{libheader|gintro}}
{{libheader|gintro}}
As we need to catch the key press events, we give an example inside a Gtk3 application.
As we need to catch the key press events, we give an example inside a Gtk3 application.
<lang Nim>import tables
<syntaxhighlight lang="nim">import tables


import gintro/[glib, gobject, gio]
import gintro/[glib, gobject, gio]
Line 647: Line 647:
let app = newApplication(App, "Rosetta.KeyboardMacros")
let app = newApplication(App, "Rosetta.KeyboardMacros")
discard app.connect("activate", activate)
discard app.connect("activate", activate)
discard app.run()</lang>
discard app.run()</syntaxhighlight>


=={{header|Oz}}==
=={{header|Oz}}==
Window-specific key bindings:
Window-specific key bindings:
<lang oz>declare
<syntaxhighlight lang="oz">declare
[QTk] = {Module.link ['x-oz://system/wp/QTk.ozf']}
[QTk] = {Module.link ['x-oz://system/wp/QTk.ozf']}
Label
Label
Line 664: Line 664:
action:toplevel#close
action:toplevel#close
)}
)}
{Window show}</lang>
{Window show}</syntaxhighlight>


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use Term::ReadKey;
use Term::ReadKey;
Line 705: Line 705:
}
}


ReadMode 0; # reset the terminal to normal mode</lang>
ReadMode 0; # reset the terminal to normal mode</syntaxhighlight>


=={{header|Phix}}==
=={{header|Phix}}==
Line 714: Line 714:
a general key handler.
a general key handler.
Obviously K_C and K_F2 could be swapped without any problem.
Obviously K_C and K_F2 could be swapped without any problem.
<!--<lang Phix>-->
<!--<syntaxhighlight lang="phix">-->
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 744: Line 744:
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</lang>-->
<!--</syntaxhighlight>-->


===system===
===system===
Line 754: Line 754:
of "keyboard macros" - though of course you could easily replace that
of "keyboard macros" - though of course you could easily replace that
SendInput call with any routine of your choosing.
SendInput call with any routine of your choosing.
<!--<lang Phix>-->
<!--<syntaxhighlight lang="phix">-->
<span style="color: #000080;font-style:italic;">--
<span style="color: #000080;font-style:italic;">--
-- demo\arwendemo\hotkey.exw
-- demo\arwendemo\hotkey.exw
Line 899: Line 899:
<span style="color: #000000;">WinMain</span><span style="color: #0000FF;">(</span><span style="color: #000000;">Main</span><span style="color: #0000FF;">,</span><span style="color: #000000;">SW_NORMAL</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">WinMain</span><span style="color: #0000FF;">(</span><span style="color: #000000;">Main</span><span style="color: #0000FF;">,</span><span style="color: #000000;">SW_NORMAL</span><span style="color: #0000FF;">)</span>
<!--</lang>-->
<!--</syntaxhighlight>-->


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
Line 906: Line 906:
global variable, 'Up' and 'Down' arrows to increment or decrement that value,
global variable, 'Up' and 'Down' arrows to increment or decrement that value,
and 'Home' to print the current value to the console.
and 'Home' to print the current value to the console.
<lang PicoLisp>(load "@lib/led.l" "@lib/term.l")
<syntaxhighlight lang="picolisp">(load "@lib/led.l" "@lib/term.l")


(fkey *XtF1
(fkey *XtF1
Line 918: Line 918:


(fkey *XtHome
(fkey *XtHome
(prinl "Current value is " *Number) )</lang>
(prinl "Current value is " *Number) )</syntaxhighlight>
Output when hitting 'F1', 'Down', 'Up', 'Up' and 'Home':
Output when hitting 'F1', 'Down', 'Up', 'Up' and 'Home':
<pre>Initialized value to 1
<pre>Initialized value to 1
Line 929: Line 929:
PureBasic has support for shortcut/macro creation in any window that supports events. This allows for creation of both single and combinations as shown in the code below.
PureBasic has support for shortcut/macro creation in any window that supports events. This allows for creation of both single and combinations as shown in the code below.
For full set of combinations on PC, Mac & Linux please see the official manual, [http://www.purebasic.com/documentation/window/addkeyboardshortcut.html here].
For full set of combinations on PC, Mac & Linux please see the official manual, [http://www.purebasic.com/documentation/window/addkeyboardshortcut.html here].
<lang PureBasic>#Win = 0
<syntaxhighlight lang="purebasic">#Win = 0
#Demo1 = 0
#Demo1 = 0
#Demo2 = 1
#Demo2 = 1
Line 954: Line 954:
EndSelect
EndSelect
ForEver
ForEver
EndIf</lang>
EndIf</syntaxhighlight>


=={{header|Python}}==
=={{header|Python}}==
Works on Unix platforms.
Works on Unix platforms.


<lang python>#!/usr/bin/env python
<syntaxhighlight lang="python">#!/usr/bin/env python
import curses
import curses


Line 980: Line 980:
curses.echo()
curses.echo()
curses.endwin()
curses.endwin()
</syntaxhighlight>
</lang>


=={{header|Racket}}==
=={{header|Racket}}==


<lang racket>
<syntaxhighlight lang="racket">
#lang racket
#lang racket


Line 1,039: Line 1,039:
macros '())]
macros '())]
[else (write-byte b) (flush-output) (loop inps v '())])))))
[else (write-byte b) (flush-output) (loop inps v '())])))))
</syntaxhighlight>
</lang>


=={{header|Raku}}==
=={{header|Raku}}==
(formerly Perl 6)
(formerly Perl 6)
<lang perl6>my $TTY = open("/dev/tty");
<syntaxhighlight lang="raku" line>my $TTY = open("/dev/tty");
my @INPUT;
my @INPUT;


Line 1,089: Line 1,089:
default { log "Unrecognized key: $_"; }
default { log "Unrecognized key: $_"; }
}
}
}</lang>
}</syntaxhighlight>


=={{header|REBOL}}==
=={{header|REBOL}}==
<lang REBOL>REBOL [
<syntaxhighlight lang="rebol">REBOL [
Title: "Keyboard Macros"
Title: "Keyboard Macros"
URL: http://rosettacode.org/wiki/Keyboard_macros
URL: http://rosettacode.org/wiki/Keyboard_macros
Line 1,119: Line 1,119:
pad 1x100 return
pad 1x100 return
text "(c) 1977 G. Beker"
text "(c) 1977 G. Beker"
]</lang>
]</syntaxhighlight>


=={{header|REXX}}==
=={{header|REXX}}==
Line 1,130: Line 1,130:
<br><br>REXX programs not included are '''$T''' which is only used when specific options are used (used when TOPS is specified),
<br><br>REXX programs not included are '''$T''' which is only used when specific options are used (used when TOPS is specified),
<br>the '''$ERR''' program which issues errors, and '''$H''' which shows '''help''' and other documentation.
<br>the '''$ERR''' program which issues errors, and '''$H''' which shows '''help''' and other documentation.
<lang rexx>/*REXX program can re-define most keys (including F keys) on a PC keyboard.*/
<syntaxhighlight lang="rexx">/*REXX program can re-define most keys (including F keys) on a PC keyboard.*/
trace off
trace off
parse arg !
parse arg !
Line 1,729: Line 1,729:
syntax: !sigl=sigl; call er 13,!FID(2) !FID(3) !sigl !cal() condition('D') sourceline(!sigl)
syntax: !sigl=sigl; call er 13,!FID(2) !FID(3) !sigl !cal() condition('D') sourceline(!sigl)
whenstamp: arg whenFID; call lineout whenFID,strip(left(date('U'),6)left(date("S"),4) time() arg(2)); call lineout whenFID,' '; call lineout whenFID; return
whenstamp: arg whenFID; call lineout whenFID,strip(left(date('U'),6)left(date("S"),4) time() arg(2)); call lineout whenFID,' '; call lineout whenFID; return
</syntaxhighlight>
</lang>


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
load "guilib.ring"
load "guilib.ring"


Line 1,756: Line 1,756:
on 16777265 see "You pressed F2 " + nl
on 16777265 see "You pressed F2 " + nl
off
off
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 1,767: Line 1,767:
Here's a sample from the Shoes manual showing how to capture key sequences. This is application specific.
Here's a sample from the Shoes manual showing how to capture key sequences. This is application specific.


<lang ruby>Shoes.app do
<syntaxhighlight lang="ruby">Shoes.app do
@info = para "NO KEY is PRESSED."
@info = para "NO KEY is PRESSED."
keypress do |k|
keypress do |k|
@info.replace "#{k.inspect} was PRESSED."
@info.replace "#{k.inspect} was PRESSED."
end
end
end</lang>
end</syntaxhighlight>


A more specific example, using some emacs bindings
A more specific example, using some emacs bindings


<lang ruby>Shoes.app do
<syntaxhighlight lang="ruby">Shoes.app do
keypress do |key|
keypress do |key|
case key
case key
Line 1,798: Line 1,798:
end
end
end
end
end</lang>
end</syntaxhighlight>


=={{header|Scala}}==
=={{header|Scala}}==
===Java Swing Interoperability===
===Java Swing Interoperability===
{{libheader|Scala Java Swing interoperability}}
{{libheader|Scala Java Swing interoperability}}
<lang Scala>import java.awt.event.{KeyAdapter, KeyEvent}
<syntaxhighlight lang="scala">import java.awt.event.{KeyAdapter, KeyEvent}


import javax.swing.{JFrame, JLabel, WindowConstants}
import javax.swing.{JFrame, JLabel, WindowConstants}
Line 1,826: Line 1,826:
}
}


}</lang>
}</syntaxhighlight>


=={{header|Tcl}}==
=={{header|Tcl}}==
{{libheader|Tk}}
{{libheader|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).
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
<syntaxhighlight lang="tcl">package require Tk
# Show off some emacs-like bindings...
# Show off some emacs-like bindings...
pack [label .l -text "C-x C-s to save, C-x C-c to quit"]
pack [label .l -text "C-x C-s to save, C-x C-c to quit"]
Line 1,838: Line 1,838:
tk_messageBox -message "We would save here"
tk_messageBox -message "We would save here"
}
}
bind . <Control-x><Control-c> {exit}</lang>
bind . <Control-x><Control-c> {exit}</syntaxhighlight>
===Key-to-key mapping macros===
===Key-to-key mapping macros===
A more direct macro-like facility (substituting one key sequence for another) would be:
A more direct macro-like facility (substituting one key sequence for another) would be:
<lang tcl>bind . <F1> {
<syntaxhighlight lang="tcl">bind . <F1> {
foreach c [split "Macro demo!" {}] {
foreach c [split "Macro demo!" {}] {
event generate %W $c
event generate %W $c
}
}
}</lang>
}</syntaxhighlight>
This can then be wrapped up like this:
This can then be wrapped up like this:
<lang tcl>package require Tk
<syntaxhighlight lang="tcl">package require Tk
proc sendMacro {w string} {
proc sendMacro {w string} {
foreach c [split $string {}] {
foreach c [split $string {}] {
Line 1,862: Line 1,862:
macro F2 "You pressed the F2 key"
macro F2 "You pressed the F2 key"
macro F3 "I'm getting bored here..."
macro F3 "I'm getting bored here..."
pack [text .t]; # A place for you to test the macros</lang>
pack [text .t]; # A place for you to test the macros</syntaxhighlight>


=={{header|Vedit macro language}}==
=={{header|Vedit macro language}}==
<lang vedit>// Configure a key to access menu item.
<syntaxhighlight 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.
// 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.
// This has the advantage that the key binding is shown in the menu.
Line 1,883: Line 1,883:


// Remove a key assignment. If INSERT option was used when the key was assigned, the old assignment will come in effect again.
// 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>
Key_Delete("Ctrl-Shft-N")</syntaxhighlight>


=={{header|Wren}}==
=={{header|Wren}}==
Line 1,890: Line 1,890:
<br>
<br>
As it's not currently possible for Wren-cli to access Xlib directly, we embed a Wren script in a C application to complete this task.
As it's not currently possible for Wren-cli to access Xlib directly, we embed a Wren script in a C application to complete this task.
<lang ecmascript>/* keyboard_macros.wren */
<syntaxhighlight lang="ecmascript">/* keyboard_macros.wren */


var GrabModeAsync = 1
var GrabModeAsync = 1
Line 1,949: Line 1,949:
xd.ungrabKey(xd.keysymToKeycode(X.stringToKeysym("F7")), Mod1Mask, drw)
xd.ungrabKey(xd.keysymToKeycode(X.stringToKeysym("F7")), Mod1Mask, drw)
xd.ungrabKey(xd.keysymToKeycode(X.stringToKeysym("F6")), Mod1Mask, drw)
xd.ungrabKey(xd.keysymToKeycode(X.stringToKeysym("F6")), Mod1Mask, drw)
xd.closeDisplay()</lang>
xd.closeDisplay()</syntaxhighlight>
<br>
<br>
We now embed this Wren script in the following C program, compile and run it.
We now embed this Wren script in the following C program, compile and run it.
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
Line 2,127: Line 2,127:
free(script);
free(script);
return 0;
return 0;
}</lang>
}</syntaxhighlight>


{{omit from|ACL2}}
{{omit from|ACL2}}