10 examples of 'python send keystrokes' in Python

Every line of 'python send keystrokes' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
49def KeyStroke(event):
50
51 global current_window
52
53 # check to see if target hanged windows
54 if event.WindowName != current_window:
55 current_window = event.WindowName
56 get_current_process
57
58 # check if they pressed standard key
59 if event.Ascii > 32 and event.Ascii < 127:
60 print chr(event.Ascii),
61 else:
62 # check if ctrl-v has been used, if so get clipboard
63 if event.Key == "V":
64
65 win32clipboard.OpenClipboard()
66 pasted_value = win32clipboard.GetClipboardData()
67 win32clipboard.CloseClipboard()
68
69 print "[PASTE] - %s" % (pasted_value),
70
71 else:
72
73 print "[%s]" % event.Key
74
75 # pass execution to next hook registered
76 return True
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

402def sendShiftedKey(key, times = 1):
403 shiftDown()
404 sendKey(key, times = times, extended = True)
405 shiftUp()
359def test_send_keystrokes_virtual_keys_ctrl(self):
360 testString = "^a^c{RIGHT}^v"
361
362 self.dlg.minimize()
363 self.dlg.Edit.send_keystrokes(testString)
364
365 actual = self.dlg.Edit.texts()[0]
366 expected = "and the note goes here ...and the note goes here ..."
367 self.assertEqual(expected, actual)
411def send_char(self, key_char):
412 print(f"sending char: {key_char}")
413 self.input['keychar'] = key_char
414 self.input['keycode'] = 0
415 self.input['special_key'] = 0
1632@cmdutils.register(instance='command-dispatcher', scope='window')
1633def fake_key(self, keystring, global_=False):
1634 """Send a fake keypress or key string to the website or qutebrowser.
1635
1636 :fake-key xy - sends the keychain 'xy'
1637 :fake-key <Ctrl-x> - sends Ctrl-x
1638 :fake-key <Escape> - sends the escape key
1639
1640 Args:
1641 keystring: The keystring to send.
1642 global_: If given, the keys are sent to the qutebrowser UI.
1643 """
1644 try:
1645 sequence = keyutils.KeySequence.parse(keystring)
1646 except keyutils.KeyParseError as e:
1647 raise cmdutils.CommandError(str(e))
1648
1649 for keyinfo in sequence:
1650 press_event = keyinfo.to_event(QEvent.KeyPress)
1651 release_event = keyinfo.to_event(QEvent.KeyRelease)
1652
1653 if global_:
1654 window = QApplication.focusWindow()
1655 if window is None:
1656 raise cmdutils.CommandError("No focused window!")
1657 QApplication.postEvent(window, press_event)
1658 QApplication.postEvent(window, release_event)
1659 else:
1660 tab = self._current_widget()
1661 tab.send_event(press_event)
1662 tab.send_event(release_event)
91def send(self, k):
92 self.trace.append(k)
93 if self._on_key:
94 self._on_key(self.trace)
95 match = False
96 for m, (t, cbs) in self._cur.items():
97 if m(k):
98 self._cur = t
99 if cbs:
100 match = True
101 if self._on_match:
102 self._on_match(self.trace)
103 for cb in cbs:
104 cb(self.trace)
105 if not match and self._on_nomatch:
106 self._on_nomatch(self.trace)
107 tr = self.trace
108 if len(self._cur) == 0 or not match:
109 self.reset()
110 if len(tr) > 1 and not match:
111 self.send(k)
342def send_keys(self, keys):
343 return self.display.send_keys([Gdk.keyval_from_name(k) for k in keys])
482def send_chars(self,
483 chars,
484 with_spaces=True,
485 with_tabs=True,
486 with_newlines=True):
487 """
488 Silently send a character string to the control in an inactive window
489
490 If a virtual key with no corresponding character is encountered
491 (e.g. VK_LEFT, VK_DELETE), a KeySequenceError is raised. Consider using
492 the method send_keystrokes for such input.
493 """
494 input_locale_id = win32functions.GetKeyboardLayout(0)
495 keys = keyboard.parse_keys(chars, with_spaces, with_tabs, with_newlines)
496
497 for key in keys:
498 key_info = key.get_key_info()
499 flags = key_info[2]
500
501 unicode_char = (
502 flags & keyboard.KEYEVENTF_UNICODE ==
503 keyboard.KEYEVENTF_UNICODE)
504
505 if unicode_char:
506 _, char = key_info[:2]
507 vk = win32functions.VkKeyScanExW(chr(char), input_locale_id) & 0xFF
508 scan = win32functions.MapVirtualKeyW(vk, 0)
509 else:
510 vk, scan = key_info[:2]
511 char = win32functions.MapVirtualKeyW(vk, 2)
512
513 if char > 0:
514 lparam = 1 << 0 | scan << 16 | (flags & 1) << 24
515 win32api.SendMessage(self.handle, win32con.WM_CHAR, char, lparam)
516 else:
517 raise keyboard.KeySequenceError(
518 'no WM_CHAR code for {key}, use method send_keystrokes instead'.
519 format(key=key))
320def keypress(event):
321
322 # Strip literal quotes from key symbol
323 keysym = repr(event.keysym)[1:-1]
324
325 # Assume no arrow keys pressed
326 event.widget.axis_x, event.widget.axis_y = 0,0
327
328 # Check all arrow keys
329 if keysym == 'Right':
330 event.widget.axis_x = +1
331 elif keysym == 'Left':
332 event.widget.axis_x = -1
333 if keysym == 'Up':
334 event.widget.axis_y = +1
335 elif keysym == 'Down':
336 event.widget.axis_y = -1
337
338 # Spacebar toggles autopilot
339 elif keysym == 'space':
340 event.widget.autopilot = not event.widget.autopilot
341
342 # If any arrow key was pressed, axes have been set
343 if event.widget.axis_x or event.widget.axis_y:
344
345 # Record arrow-key press time for fake release
346 event.widget.lastpress_sec = time.time()
347
348 # Axis control disables autopilot
349 event.widget.autopilot = False
338def sendAltKey(key):
339 SendInput(Keyboard(VK_ALT), Keyboard(key))
340 time.sleep(0.2)
341 SendInput(Keyboard(VK_ALT, KEYEVENTF_KEYUP),
342 Keyboard(key, KEYEVENTF_KEYUP))
343 time.sleep(0.2)

Related snippets