#!/bin/sh # # keyboard_macro [-r] "string" # keyboard_macro [-r] - # # Inject a keyboard macro string into my X windows input stream # either by reading from standard input, or as a command line argument # # This uses "XTEST" for global event injection, instead of "XSendEvents" # for the event injection. # # OPTIONS # -r add a final newline (or Return key) to the typed string # # EXAMPLES # # My Email Address # keyboard_macro "Anthony.Thyssen@gmail.com" # # My Public Web Site # keyboard_macro "https://antofthy.gitlab.io/" # # Grab my current text selection to clipboard (Ctrl-C doesn't work, xterm) # I assign this to a 'Win-C' key # sh -c 'xsel -p | xsel -i -b' # # Paste the clipboard into ANY input window (Ctrl-V doesn't work, xterms) # I assign this to a 'Win-V' key # sh -c 'xsel -b | keyboard_macro - # # DIY Version, (from command line with time delay so you can focus)) # sleep 5; xdotool type string # or echo "sleep 5; type string" | xdotool - # #### # # Note: that I sometimes include a 1/2 second delay before the macro is 'typed' # so that any modifiers the user has used to initiate the keyboard macro has # been released. Or I try to 'keyup' those modifier keys. # # For Development and other information on injected events into X windows # See https://antofthy.gitlab.io/info/X/event_handling.txt # # Anthony Thyssen 2012 # if [ "X$1" = "X-r" ]; then shift RETURN=true fi #method=xte method=xdotool case $method in xte) # WARNING:- "xte" version 1.02 truncates to 255 characters. # On the other hand "xte" version 1.07 & 1.09 ignores all newlines in the # string! No later version is available as of Sept 2015. # # The following perl function is a work around, originally provided the # by author, and improved opon my me. Basically it splits up the input to # chunks of 200 characters and handles returns seperatally. # # But it cannot type unicode characters like '§' # if [ "X$1" = "X-" ]; then perl -e ' # Handle the "str" command of xte, # interspersed with the appropriate "key Returns" $str=0; $count=0; sub str_on() { if (!$str ) { $str=1; print "str "; $count=0 } } sub str_off() { if ( $str ) { $str=0; print "\n"; } } print "keyup Super_L\n"; # ensure shift modifier keys up print "keyup Control_L\n"; print "keyup Alt_L\n"; print "keyup v\n"; # as well as the launch key! # #print "usleep 500000\n"; # or give time for user to release key while ( <> ) { for my $char ( split( // ) ) { if( $char eq "\n" ) { str_off(); print "key Return\n"; } else { str_on(); print $char; if( $count++ > 200 ) { str_off(); } } } } str_off(); print "key Return\n" if length("'"$RETURN"'") ' - | xte else # This is for short strings, given on the command line (macros) # it should not be used for long strings due to the problems given above. xte 'keyup Super_L' 'keyup Control_L' 'keyup Alt_L' 'keyup v' \ "str $1" \ ${RETURN:+'key Return'} fi ;; xdotool) # Use xdotool which has clear modifiers. # WARNING before v2015, 'clearmodifiers' restores the modifier afterwards! # # xdotool can type unicode symbols like '§' which is done by keyboard # switching. # Ensure any and all keys that may have initiated the macro # are no longer pressed. xdotool keyup Super_L keyup Control_L keyup Alt_L keyup v # # delay is used to slow input. EG 10ms between keys if [ "X$1" = "X-" ]; then xdotool type --clearmodifiers -delay 10 --file - else echo -n "$1" | xdotool type --clearmodifiers -delay 10 --file - fi if [ "X$RETURN" != 'X' ]; then xdotool key Return fi # without --clearmodifiers use... #xdotool sleep 0.5 type -delay 0 "$1" ;; esac