Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add modifiers support for keypress #663

Merged
merged 2 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading