Module: XDo::Keyboard
- Defined in:
- lib/xdo/keyboard.rb
Overview
A namespace encabsulating methods to simulate keyboard input. You can send input to special windows, use the w_id
parameter of many methods for that purpose. NOTE: xdotool seams to reject the --window
option even if you try to run it directly. This command fails (where 60817411 is a window id):
xdotool key --window 60817411 a
So don’t be surprised if it does not work with this library. Hopefully this will be fixed, so I leave this in.
Constant Summary collapse
- SPECIAL_CHARS =
Special escape chars. The list is fairly uncomplete, so feel free to add chars not noted. Use the program
xev
to determine how a sequence is escaped and map the char to the escape sequence. { "\n" => "Return", " " => "space", "?" => "shift+question", "!" => "shift+exclam", "," => "comma", "." => "period", ";" => "shift+semicolon", ":" => "shift+colon", '"' => "shift+2", "§" => "shift+3", "$" => "shift+4", "%" => "shift+5", "&" => "shift+6", "/" => "shift+7", "(" => "shift+8", ")" => "shift+9", "=" => "shift+0", "´" => "dead_acute+dead_acute", "`" => "shift+dead_grave+shift+dead_grave", "^" => "dead_circumflex+dead_circumflex", "°" => "shift+degree", "+" => "plus", "*" => "shift+asterisk", "#" => "numbersign", "'" => "shift+apostrophe", "-" => "minus", "_" => "shift+underscore", "<" => "less", ">" => "shift+greater", "\t" => "Tab", "\b" => "BackSpace", "ä" => "adiaeresis", "Ä" => "shift+adiaeresis", "ö" => "odiaeresis", "Ö" => "shift+odiaeresis", "ü" => "udiaeresis", "Ü" => "shift+udiaeresis", "ß" => "ssharp", "¹" => "ISO_Level3_Shift+1", "²" => "ISO_Level3_Shift+2", "³" => "ISO_Level3_Shift+3", "¼" => "ISO_Level3_Shift+4", "½" => "ISO_Level3_Shift+5", "¬" => "ISO_Level3_Shift+6", "{" => "ISO_Level3_Shift+7", "[" => "ISO_Level3_Shift+8", "]" => "ISO_Level3_Shift+9", "}" => "ISO_Level3_Shift+0", "\\" => "ISO_Level3_Shift+backslash", "@" => "ISO_Level3_Shift+at", "€" => "ISO_Level3_Shift+EuroSign", "~" => "ISO_Level3_Shift+dead_tilde+IS_Level3_Shift+dead_tilde+Left+BackSpace", #xdotool seems to have a bug here, so I need to delete the extra plus sign. "|" => "ISO_Level3_Shift+bar" }
Class Method Summary collapse
-
.char(c, w_id = nil) ⇒ Object
(also: key)
Simulate a single char directly via the
key
function ofxdotool
. -
.delete(right = false) ⇒ Object
Deletes a char.
-
.key_down(key, w_id = nil) ⇒ Object
Holds a key down.
-
.key_up(key, w_id = nil) ⇒ Object
Releases a key hold down by #key_down.
-
.method_missing(sym, *args, &block) ⇒ Object
Allows you to things like this: XDo::Keyboard.ctrl_c The string will be capitalized and every _ will be replaced by a + and then passed into #char.
-
.simulate(str, raw = false, w_id = nil) ⇒ Object
Types a character sequence.
-
.type(str, w_id = nil) ⇒ Object
Types a character sequence, but without any special chars.
Class Method Details
.char(c, w_id = nil) ⇒ Object Also known as: key
Simulate a single char directly via the key
function of xdotool
. c
is a single char like “a” or a combination like “shift+a”.
213 214 215 |
# File 'lib/xdo/keyboard.rb', line 213 def char(c, w_id = nil) `#{XDOTOOL} key #{w_id ? "--window #{w_id} " : ""}#{c}` end |
.delete(right = false) ⇒ Object
Deletes a char. If right
is true, del_char
uses the DEL key for deletion, otherwise the BackSpace key.
230 231 232 |
# File 'lib/xdo/keyboard.rb', line 230 def delete(right = false) Keyboard.simulate(right ? "\b" : "{DEL}") end |
.key_down(key, w_id = nil) ⇒ Object
Holds a key down. Please call #key_up after a call to this method.
219 220 221 |
# File 'lib/xdo/keyboard.rb', line 219 def key_down(key, w_id = nil) `#{XDOTOOL} keydown #{w_id ? "--window #{w_id} " : "" }#{check_for_special_key(key)}` end |
.key_up(key, w_id = nil) ⇒ Object
Releases a key hold down by #key_down.
224 225 226 |
# File 'lib/xdo/keyboard.rb', line 224 def key_up(key, w_id = nil) `#{XDOTOOL} keyup #{w_id ? "--window #{w_id} " : "" }#{check_for_special_key(key)}` end |
.method_missing(sym, *args, &block) ⇒ Object
238 239 240 241 |
# File 'lib/xdo/keyboard.rb', line 238 def method_missing(sym, *args, &block) super if args.size > 1 or block char(sym.to_s.capitalize.gsub("_", "+"), args[0]) end |
.simulate(str, raw = false, w_id = nil) ⇒ Object
Types a character sequence. You can use the escape sequence … to send special keystrokes, a full list of supported keystrokes is printed out by:
puts (XDo::Keyboard.constants - [:SPECIAL_CHARS])
This method recognizes many special chars like ? and ä, even if you disable the escape syntax .. via setting the raw
parameter to true (that’s the only way to send the { and } chars). It’s a bit slower than the #type method.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/xdo/keyboard.rb', line 162 def simulate(str, raw = false, w_id = nil) if raw commands = [] str.each_char do |char| commands << "#{XDOTOOL} key #{w_id ? "--window #{w_id} " : ""}#{check_for_special_key(char)}" end commands.each do |cmd| out = `#{cmd}` end return nil else raise(XDo::XError, "Invalid number of open and close braces!") unless str.scan(/{/).size == str.scan(/}/).size tokens = [] if str =~ /{/ str.scan(/(.*?){(\w+)}/){|normal, escape| tokens << normal << escape} #Wenn noch Text übrig ist, diesen noch dranhängen. tokens << $' unless $'.empty? else #Keine Escape-Sequenz drin, also ist eine Spezialbehandlung überflüssig. #Rekursiver Aufruf von simulate, aber als reiner Text. simulate(str, true, w_id) return end commands = [] tokens.each_with_index do |token, index| #Jeder zweite Token muss eine Escape-Sequenz sein; selbst wenn der #String mit einer Escape-Sequenz anfängt, wird der Reguläre Ausdruck #einen Leerstring extrahieren. if index.odd? #Ein Array fängt bei Null an, das zweite Element ist Eins. #Escape-Sequenz in eine Befehlsfolge umwandeln commands << sequence_escape(token, w_id) #Die eingefügten Arrays plätten, damit ein einziger Befehlsstack übrig bleibt commands.flatten! else #Ab hier ist der Token sicherlich normaler Text; entsprechend wird damit verfahren. token.each_char do |char| commands << "#{XDOTOOL} key #{w_id ? "--window #{w_id} " : ""}#{check_for_special_key(char)}" end commands.flatten! #Zur Sicherheit end end #Ausführen aller Befehle commands.each do |cmd| out = `#{cmd}` end end end |
.type(str, w_id = nil) ⇒ Object
Types a character sequence, but without any special chars. This function is a bit faster then #simulate.
151 152 153 154 |
# File 'lib/xdo/keyboard.rb', line 151 def type(str, w_id = nil) out = `#{XDOTOOL} type #{w_id ? "--window #{w_id} " : ""}"#{str}"` nil end |