Detect "end: key press in python3 - python-3.5

I am trying to take input infinitely from user until the user presses end key on the key board. The input can be any data type. for example code below I want to exit the while loop when end is pressed on the key board.
l = []
while 1:
v = input("Enter the list element")
l.append(v)
if int(v)==1: # instead of checking 1 I want to check for "end" key press
break
print("the list is %s" %(l))
Would be great if someone can suggest exit on combination of key press as well such as ctrl+e etc.

You can use pynput library to detect which key was pressed
I believe Input() could not detect functional keys, (except for enter or backspace, and etc.)
They have pynput.keyboard.Listener to listen to keys
Additional Info

Youcan use Exception Handling.
try:
While 1:
# your code
except KeyboardInterrupt:
print("Exit")
exit()
This code will loop until you give an keyboard interrupt (ctrl+c)..
For more information refer: https://www.python-course.eu/exception_handling.php

Related

How to detect key press on a terminal while not actually generating those keystrokes as inputs? PYTHON

My goal was to have the user presses 2 keys at the same time to continue the program. However, in my actual code, there is an input prompt following the detection of the keystrokes. Then, those 2 keys will appear on the in-line text field, then the user has to erase that before proceeding.
I have created a minimal single script to recreate that behavior:
import keyboard
import time
print("Running keyboard script")
while True:
try:
if keyboard.is_pressed('z') and keyboard.is_pressed('p'):
print('z and p press')
time.sleep(1)
something = input("Type something:") # `zp` will appear in this prompt
except KeyboardInterrupt:
print("Exiting")
break
I mean the user can just hit backspace twice to erase that, but I want to automate that part. A solution that I came up with was to use backspace characters as a workaround:
import keyboard
import time
print("Running keyboard script")
b = '\b,'*100
b+='\b'
while True:
try:
if keyboard.is_pressed('z') and keyboard.is_pressed('p'):
keyboard.press_and_release(b) # I added this as a workaround, however, it introduces different behavior
print('z and p press')
time.sleep(1)
something = input("Type something:")
except KeyboardInterrupt:
print("Exiting")
break
This solution seems to work if and only if the user's active window is the terminal. If the user clicks away to a different window, the backspace key press will be sent to the keyboard controller and it will erase 101 characters of the other focused window, for instance, if they click away to a text editor, then 101 characters would be erased. 101 characters is just a magic number that I came up in case they pressed more than the two keys that I asked.
So, I wonder if there is a better solution to just detecting the keyboard press while blocking them from being sent to the input prompt? I have tried, sys.stdin.read() but apparently, it won't read anything until the contents have been entered. Also, tried io.StringIO(sys.stdin.read()), which behaves similar to sys.stdin.read()

Allow hotkey that presses a disabled key

My keyboard key/letter f is ruined and continuously presses the letter. I am using AutoHotKey to successully disable the letter, but I need an alternative hotkey to press the key. I have chosen to use CTRL+j as the hotkey to press the letter f. I tried this script but it does not seem to press the key:
f::return
^j::
Send f
return
I also tried this but it also does not work:
f::return
^j::
Send {f down}
return
How can I get the script to press f using the hotkey CTRL+j, while disabling the key f?
The $ modifier is going to solve this problem.
That modifier makes it so that your f::return hotkey wont affect keys that are sent from AHK.
So here's your finished script:
$f::return
^j::SendInput, f
Also switched over to SendInput due to it being the recommended faster and more reliable send mode.
I used the ASCII code for the letters in both uppercase and lowercase, and it also worked:
; disable the key 'f':
f::return
; press CTRL+j to press f:
^j::
Send {Asc 102}
return
; press CTRL+Shift+j to press F:
^+j::
Send {Asc 70}
return

Pyautogui - When using code that use special keys, the key seem stuck at times

I am getting a weird behavior while using pyautogui hotkey; where the special key used at times does "stick" and cause the subsequent text printed with pyautogui.typewrite() function to be printed incorrectly.
For example, this statement ran on OSX will cause shift to stick at random; causing the output to be printed capitalized.
pyautogui.hotkey("command", "shift", "f")
time.sleep(3)
pyautogui.typewrite("reference_file2.txt")
This will open the "recents" window in Finder, wait 3 seconds then will type a filename which should (or should not be) in the recents window. Sometimes the output of typewrite is printed like if Shift key was pressed.
So instead of
reference_file2.txt
You will see
REFERENCE_FILE#>TXT
which is exactly what you get if you type the original string with Shift key pressed.
Is this a bug in the hotkey function of pyautogui? Or am I supposed to do something to ensure that the keys pressed with hotkey is released, before moving on with the next statement? The documentation of pyautogui specify that the hotkey function does equal to a keypress and keyrelease sequence, so no action should be required, right?

Cursor position in the IDLE

I just wrote my first program in python, but when I launch it on the IDLE the cursor immediately starts a line below my print() statement. Is there a way to set the cursor to start next to my print statement? Also I have noticed when I ran different programs that take user input, sometimes the cursors will appear above my print(). Is there any fixes to this?
When you prompt for input, you can put the prompt in the input call.
>>> name = input('What is your name? ')
What is your name? |
print() in Python, by default inserts a new line. In order to modify this behaviour, you have to explicitly call print with following args
print(string, end="")
Source:-
https://docs.python.org/3/library/functions.html#print

g++ : How can I get keystate on linux

I Want to get keystate for some key like shift. If I press "a" with shift to write "A" then by g++ program on linux how can I ensure that shift is also pressed while I press key "a".
Thanks in advance.
See the answer for this question: What is Equivalent to getch() & getche() in Linux?
It will give you the correct keycodes, i.e. 'a' if you press 'a' with shift depressed, 'A' if you press it with shift pressed, etc. Getting the state of the shift key alone is a different matter though, I'm not sure if there's an easy way to do that.

Resources