Class: Osascript::Key
- Inherits:
-
Object
- Object
- Osascript::Key
- Defined in:
- lib/osascript/Key.rb
Constant Summary collapse
- KEY2CODE =
/<< self Key
{ down_arrow: 125, DOWN_ARROW: 125, DOWN: 125, up_arrow: 126, UP_ARROW: 126, UP: 126, left_arrow: 123, LEFT_ARROW: 123, LEFT: 123, right_arrow: 124, RIGHT_ARROW: 124, RIGHT: 124, enter: 76, ENTER: 76, return: 36, RETURN: 36, RET: 36, backspace: 51, BACKSPACE: 51, escape: 53, ESCAPE: 53, ESC: 53, space: 49, SPACE: 49, caps_lock: 57, CAPS_LOCK: 57, tab: 48, TAB: 48 }
Class Method Summary collapse
- .code_system_events(keys, options) ⇒ Object
- .key_to_sysevents_code(key) ⇒ Object
- .press(keys, app = nil, options = nil) ⇒ Object
- .press_with_app(keys, app, options) ⇒ Object
- .press_without_app(keys, options) ⇒ Object
-
.rationalize_keys(keys) ⇒ Object
key
must contain only Integer, Float, String or Hash.
Class Method Details
.code_system_events(keys, options) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/osascript/Key.rb', line 111 def code_system_events(keys, ) keys = [keys] unless keys.is_a?(Array) keys_stroke = keys.map do |key| "delay #{[:delay]}\n#{key_to_sysevents_code(key)}" end.join("\n") <<~CODE tell application "System Events" #{keys_stroke} end tell CODE end |
.key_to_sysevents_code(key) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/osascript/Key.rb', line 123 def key_to_sysevents_code(key) if key.is_a?(Hash) modifiers = key[:modifiers] delay_sup = key[:delay] # délai ajouté key = key[:key] else modifiers = nil delay_sup = nil end code = case key when String key = key.gsub(/"/, '\\\"') unless key.match?(/\\"/) # key = key.gsub(/\~/, '\\~') "keystroke \"#{key}\"" when Integer "key code #{key}" when Symbol "key code #{KEY2CODE[key]}" else raise "Unvalid class pour key: #{key.class}" end if modifiers modifiers = modifiers.map {|mod| "#{mod} down" }.join(', ') code = "#{code} using {#{modifiers}}" end if not(delay_sup.nil?) code = "delay #{delay_sup}\n#{code}" end return code end |
.press(keys, app = nil, options = nil) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/osascript/Key.rb', line 70 def press(keys, app = nil, = nil) ||= {} .key?(:delay) || .merge!(delay: 0.5) # Rationalize keys to not contain Array keys = rationalize_keys(keys) if app.nil? press_without_app(keys, ) else press_with_app(keys, app, ) end end |
.press_with_app(keys, app, options) ⇒ Object
94 95 96 97 98 99 100 101 102 |
# File 'lib/osascript/Key.rb', line 94 def press_with_app(keys, app, ) code = <<~CODE activate activate #{code_system_events(keys, )} CODE # puts "code :\n#{code}" Osascript::__asrun(code, app) end |
.press_without_app(keys, options) ⇒ Object
104 105 106 107 108 109 |
# File 'lib/osascript/Key.rb', line 104 def press_without_app(keys, ) code = <<~CODE #{code_system_events(key, )} CODE Osascript::__asrun(code) end |
.rationalize_keys(keys) ⇒ Object
key
must contain only Integer, Float, String or Hash
83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/osascript/Key.rb', line 83 def rationalize_keys(keys) return keys unless keys.is_a?(Array) only_keys = [] keys.each do |ii| case ii when Array then only_keys += rationalize_keys(ii) else only_keys << ii end end return only_keys end |