Skip to content

Commit

Permalink
Merge pull request #663 from MaryamHuntsperger/keyPress
Browse files Browse the repository at this point in the history
Add modifiers support for keypress
  • Loading branch information
claud-io authored Oct 30, 2024
2 parents 2bfce8b + 4832727 commit aa58ff0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
18 changes: 10 additions & 8 deletions internal/devtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1313,15 +1313,16 @@ def key_info(self, key):
info['text'] = definition['text']
return info

def key_down(self, key):
def key_down(self, key, modifier):
"""Press down a key"""
info = self.key_info(key)
params = {
'type': 'rawKeyDown',
'key': info['key'],
'windowsVirtualKeyCode': info['keyCode'],
'code': info['code'],
'location': info['location']
'location': info['location'],
'modifiers': modifier
}
if 'text' in info:
params['type'] = 'keyDown'
Expand All @@ -1331,22 +1332,23 @@ def key_down(self, key):
params['isKeypad'] = True
self.send_command('Input.dispatchKeyEvent', params)

def key_up(self, key):
def key_up(self, key, modifier):
"""Let up a key"""
info = self.key_info(key)
self.send_command('Input.dispatchKeyEvent', {
'type': 'keyUp',
'key': info['key'],
'windowsVirtualKeyCode': info['keyCode'],
'code': info['code'],
'location': info['location']
'location': info['location'],
'modifiers': modifier
})

def keypress(self, key):
def keypress(self, key, modifier):
"""Simulate pressing a keyboard key"""
try:
self.key_down(key)
self.key_up(key)
self.key_down(key, modifier)
self.key_up(key, modifier)
except Exception:
logging.exception('Error running keypress command')

Expand All @@ -1355,7 +1357,7 @@ def type_text(self, string):
try:
for char in string:
if char in self.key_definitions:
self.keypress(char)
self.keypress(char, 0)
else:
self.send_character(char)
except Exception:
Expand Down
13 changes: 12 additions & 1 deletion internal/devtools_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
import json
from .optimization_checks import OptimizationChecks

KeyModifiers = {
"ALT": 1,
"CTRL": 2,
"SHIFT": 8
}

class DevtoolsBrowser(object):
"""Devtools Browser base"""
Expand Down Expand Up @@ -765,7 +770,13 @@ def process_command(self, command):
elif command['command'] == 'type':
self.devtools.type_text(command['target'])
elif command['command'] == 'keypress':
self.devtools.keypress(command['target'])
modifier = 0
value = command['value']
if value is not None:
keyModifier = value.upper()
if keyModifier in KeyModifiers.keys():
modifier = KeyModifiers[keyModifier]
self.devtools.keypress(command['target'], modifier)
elif command['command'] == 'mouseClick':
if 'target' in command:
target = command['target']
Expand Down

0 comments on commit aa58ff0

Please sign in to comment.