Skip to content

Commit aa58ff0

Browse files
authored
Merge pull request #663 from MaryamHuntsperger/keyPress
Add modifiers support for keypress
2 parents 2bfce8b + 4832727 commit aa58ff0

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

internal/devtools.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,15 +1313,16 @@ def key_info(self, key):
13131313
info['text'] = definition['text']
13141314
return info
13151315

1316-
def key_down(self, key):
1316+
def key_down(self, key, modifier):
13171317
"""Press down a key"""
13181318
info = self.key_info(key)
13191319
params = {
13201320
'type': 'rawKeyDown',
13211321
'key': info['key'],
13221322
'windowsVirtualKeyCode': info['keyCode'],
13231323
'code': info['code'],
1324-
'location': info['location']
1324+
'location': info['location'],
1325+
'modifiers': modifier
13251326
}
13261327
if 'text' in info:
13271328
params['type'] = 'keyDown'
@@ -1331,22 +1332,23 @@ def key_down(self, key):
13311332
params['isKeypad'] = True
13321333
self.send_command('Input.dispatchKeyEvent', params)
13331334

1334-
def key_up(self, key):
1335+
def key_up(self, key, modifier):
13351336
"""Let up a key"""
13361337
info = self.key_info(key)
13371338
self.send_command('Input.dispatchKeyEvent', {
13381339
'type': 'keyUp',
13391340
'key': info['key'],
13401341
'windowsVirtualKeyCode': info['keyCode'],
13411342
'code': info['code'],
1342-
'location': info['location']
1343+
'location': info['location'],
1344+
'modifiers': modifier
13431345
})
13441346

1345-
def keypress(self, key):
1347+
def keypress(self, key, modifier):
13461348
"""Simulate pressing a keyboard key"""
13471349
try:
1348-
self.key_down(key)
1349-
self.key_up(key)
1350+
self.key_down(key, modifier)
1351+
self.key_up(key, modifier)
13501352
except Exception:
13511353
logging.exception('Error running keypress command')
13521354

@@ -1355,7 +1357,7 @@ def type_text(self, string):
13551357
try:
13561358
for char in string:
13571359
if char in self.key_definitions:
1358-
self.keypress(char)
1360+
self.keypress(char, 0)
13591361
else:
13601362
self.send_character(char)
13611363
except Exception:

internal/devtools_browser.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
import json
3131
from .optimization_checks import OptimizationChecks
3232

33+
KeyModifiers = {
34+
"ALT": 1,
35+
"CTRL": 2,
36+
"SHIFT": 8
37+
}
3338

3439
class DevtoolsBrowser(object):
3540
"""Devtools Browser base"""
@@ -765,7 +770,13 @@ def process_command(self, command):
765770
elif command['command'] == 'type':
766771
self.devtools.type_text(command['target'])
767772
elif command['command'] == 'keypress':
768-
self.devtools.keypress(command['target'])
773+
modifier = 0
774+
value = command['value']
775+
if value is not None:
776+
keyModifier = value.upper()
777+
if keyModifier in KeyModifiers.keys():
778+
modifier = KeyModifiers[keyModifier]
779+
self.devtools.keypress(command['target'], modifier)
769780
elif command['command'] == 'mouseClick':
770781
if 'target' in command:
771782
target = command['target']

0 commit comments

Comments
 (0)