I want to trigger certain functions when pressing certain arrows, it must be captured without being in console and pressing enter each time.
edit: I found now that "left arrow" registers left arrow etc BUT it triggered hundreds of times when pressed
import keyboard
while True:
if keyboard.is_pressed("left arrow"):
print("mkay")Everytime I press, it's very important that it only registeres once, any suggestions how to do that in an efficient manner?
I've got it working pretty good using sleep() efter each press but surely there must be a better way?
How to simulate pressing the arrow down key when a specific element is present in the HTML using Selenium and Python - Stack Overflow
java - Python Selenium press down arrow to dispay all page contents - Stack Overflow
click - Python, check if arrow key pressed - Stack Overflow
May I virtualization a Down arrow key pre… - Apple Community
It's the same with pyautogui, and keyboard. Just use the word for direction. For example, 'right', or 'left'. Like this:
import keyboard
keyboard.press_and_release('left') #presses left arrow key
I recommend looking at the documentation to find key codes, as to find the difference between arrows on the numpad and normal layout can be difficult, and just for general reference in the future.
You can use the below code snippet to access the arrow keys. I have made use of the keyboard library inorder to do that.
- UP ARROW KEY -> "up"
- DOWN ARROW KEY -> "down"
- LEFT ARROW KEY -> "left"
- RIGHT ARROW KEY -> "right"
Here is a sample code
import keyboard
# To Press UP ARROW KEY
keyboard.press("up")
Hope it helps!!!
Using Selenium to click on the Arrow Down key when a specific condition is met, as an example I have demonstrated through the following steps:
- Open the url https://www.google.com/
- Wait for the Google Home Page search box element i.e.
By.NAME, "q"to be clickable. - Sends the character sequence selenium.
- Wait for the auto suggestions to be visibile.
Click twice on Arrow Down key.
Code Block:
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.keys import Keys options = webdriver.ChromeOptions() options.add_argument("start-maximized") options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option('useAutomationExtension', False) driver = webdriver.Chrome(options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe') driver.get('https://www.google.com/') WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.NAME, "q"))).send_keys("Selenium") WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "ul[role='listbox'] li"))) driver.find_element_by_css_selector('body').send_keys(Keys.DOWN) driver.find_element_by_css_selector('body').send_keys(Keys.DOWN)Browser Snapshot:

PS: Implementing the above logic you can also click on Arrow Up, Arrow Left and Arrow Right keys.
you could search for the element with an xpath looking for the text you are searching e.g. $x('//*[.="Text"]') and then use sendKey() to press the key
Pressing a space would probably just scroll the page to the very bottom which probably triggers loading additional content. What you can do is is to use ActionChains() to press SPACE 8 times with a delay:
import time
from selenium.webdriver.common.action_chains import ActionChains
actions = ActionChains(driver)
for _ in range(8):
actions.send_keys(Keys.SPACE).perform()
time.sleep(1)
Or, you may scroll into view of the "footer" element (or something else in the bottom, depending on the particular web-site):
footer = driver.find_element_by_tag_name("footer")
for _ in range(8):
driver.execute_script("arguments[0].scrollIntoView();", footer)
time.sleep(1)
These are all guesses though, it is difficult to provide a reliable working solution without actually trying them out on the actual webpage you are working with.
you can use the PAGE_DOWN key 2 or 3 times
import time
from selenium.webdriver.common.action_chains import ActionChains
actions = ActionChains(driver)
for _ in range(3):
actions.send_keys(Keys.PAGE_DOWN).perform()
time.sleep(1)
In your terminal (or anacoonda prompt) run this command to install pynput library:
pip install pynput
And, in your editor
from pynput import keyboard
from pynput.keyboard import Key
def on_key_release(key):
if key == Key.right:
print("Right key clicked")
elif key == Key.left:
print("Left key clicked")
elif key == Key.up:
print("Up key clicked")
elif key == Key.down:
print("Down key clicked")
elif key == Key.esc:
exit()
with keyboard.Listener(on_release=on_key_release) as listener:
listener.join()
More info
By using listeners you will not have to play an infinite loop. Which I think is more elegant. This code will help you:
from pynput import keyboard
def on_press(key):
if key == keyboard.Key.up:
print('PRESSED')
if key == keyboard.Key.esc:
listener.stop()
with keyboard.Listener(on_press=on_press) as listener:
listener.join()
Note that with 'keyboard.Key' you can detect the key that you want. You can even reproduce the case of holding two keys at the same time and detecting a combination!
How to press all four of those keys:
from pynput.keyboard import Key, Controller
kb = Controller()
kb.press(Key.up) # Presses "up" key
kb.release(Key.up) # Releases "up" key
kb.press(Key.left) # Presses "left" key
kb.release(Key.left) #etc..
kb.press(Key.right)
kb.release(Key.right)
kb.press(Key.down)
kb.release(Key.down)
You can make it easier by creating a function if you need use it many times:
from pynput.keyboard import Key, Controller
kb = Controller()
def press(button):
kb.press(button)
kb.release(button)
# Then you can use it in one line:
press(Key.left)
# It will automatically press and release the left key.
Hope I helped.
In python, you can view enum values using the dir function.
from pynput import keyboard
print(dir(keyboard.Key)) # show full enum list
Output
['__class__', '__doc__', '__members__', '__module__', 'alt', 'alt_l', 'alt_r',
'backspace', 'caps_lock', 'cmd', 'cmd_r', 'ctrl', 'ctrl_l', 'ctrl_r', 'delete',
'down', 'end', 'enter', 'esc', 'f1', 'f10', 'f11', 'f12', 'f13', 'f14', 'f15',
'f16', 'f17', 'f18', 'f19', 'f2', 'f20', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8', 'f9',
'home', 'insert', 'left', 'media_next', 'media_play_pause', 'media_previous',
'media_volume_down', 'media_volume_mute', 'media_volume_up', 'menu', 'num_lock',
'page_down', 'page_up', 'pause', 'print_screen', 'right', 'scroll_lock', 'shift',
'shift_r', 'space', 'tab', 'up']
You can see up, down, left, right in the list. Try those keys.
The keyboard module has simple solutions for instances like these, they use event-triggered activation rather than polling as is used in your attempt.
example code:
import keyboard
def handleLeftKey(e):
if keyboard.is_pressed("4"):
print("left arrow was pressed w/ key 4")
# work your magic
keyboard.on_press_key("left", handleLeftKey)
# self-explanitory: when the left key is pressed down then do something
keyboard.on_release_key("left", handleLeftKey02)
# also self-explanitory: when the left key is released then do something
# don't use both ...on_release & ...on_press or it will be
# triggered twice per key-use (1 up, 1 down)
Replace the code below and change it to suit your needs.
if __name__ == "__main__":
while True:
code = []
try:
for key in keys:
if keyboard.is_pressed(key):
print(keyboard.key_to_scan_codes(key))
print(f"{key} pressed")
code.append(1)
else:
code.append(0)
Another, more dynamic approach would look like:
import keyboard
keys = [
"down",
"up",
"left",
"right",
"w",
"s",
"a",
"d",
"1",
"2",
"3",
"4",
"q",
"e",
"f"
]
def kbdCallback(e):
found = False
for key in keys:
if key == keyboard.normalize_name(e.name):
print(f"{key} was pressed")
found = True
# work your magic
if found == True:
if e.name == "left":
if keyboard.is_pressed("4"):
print("4 & left arrow were pressed together!")
# work your magic
keyboard.on_press(kbdCallback)
# same as keyboard.on_press_key, but it does this for EVERY key
Another issue I noticed was that you were using "left arrow" when really it was recognized as "left" (at least on my system, it may be different on yours, but I assume you want it to work on all systems so it'd be safer using "left" instead)
The last method you could use is very statically typed and has no dynamic capabilities, but would work in the case of "4+left" or "left+4"
import keyboard
def left4trigger:
print("the keys were pressed")
keyboard.add_hotkey("4+left", left4trigger)
# works as 4+left or left+4 (all of the examples do)
You seem smart enough to figure out the rest from there.
Beware, language may play a role! In my case the 'up' arrow was translated to my local language, you can run the following code to get the key value for your computer:
import keyboard
def onkeypress(event):
print(event.name)
keyboard.on_press(onkeypress)
#Use ctrl+c to stop
while True:
pass
I want to simulate a user scrolling down a web page using their down arrow key to scroll one line at a time, but I cannot seem to find a way to send keys to the chromedriver instance. All of the sendkeys code that I can find shows how to send the keys to an element and not the browser instance.