My keyboard messes up after executing my code (PYTHON) - python

For my code I am using the import keyboard, and after I execute the code, my keyboard thinks the ctrl key is pressed down and I need to press Control again for it to realize that the key isn't pressed down.
For example when I press 'A' (without really pressing on control), it selects all (like 'Ctrl + A'), or when I press 'F', it shows me the find in page thing (like 'Ctrl + F').
Just to clarify, after I execute my code, my keyboard does this stuff that I wrote here in every program.
Example for my code:
import keyboard
keyboard.press('Control+S')
keyboard.write('Hello')
keyboard.press('enter')
I run this code on google, this part of code saves an image, names it 'Hello', and then presses enter to download it.

Related

Trying to execute a part of my program everytime a key is pressed

I am using Tkinter for the first time and I am having some trouble. I want to check for a key-combination (Ctrl + the minus key on the numpad). I tried using the root.bind function but I just can't get the key combination to work. When I typed in
root.bind("<Control_L-->" lambda x: print("Hello World"))
It printed out Hello World whenever I pressed Ctrl and didn't care wether I was pressing the minus key or not. Also when I type in just - without the <Control_L> it works both for when I press minus on the keyboard and the numpad which I would like to avoid if possible.
Can anybody tell me, what to put in there so I get it to work only when I press left Ctrl and minus on the Numpad?
Another thing giving me trouble is the fact that it only checks for key presses when I am inside of the gui. If I'm inside of my browser and just have the gui open on my second monitor and press the keys it doesn't register it but I need it to work even when the app is not focused or minimized. Does anybody know how I could do that? I know about the keyboard module but when I try to use it before the root.mainloop() the gui doesn't work or if I put it after it the code checking for the key doesn't work.
if keyboard.is_pressed("ctrl + -"): print("Hello World")
You're binding to the wrong key symbol - you want Control and minus
root.bind("<Control-minus>" lambda x: print("Hello World"))

How i can do event "Click button on screen (in another application, like minecraft)" [py]

How i can do event "Click button on screen (in another application, like minecraft)"
I want make a python thing what clicking buttons on minecraft like:
'A', 'B', 'C', 'CTRL', 'SHIFT', '1', '2', '3', '\', 'ENTER' etc.
It will click only 1 time
To find people with hacks, i don't love people who cheating >:d\
Keyboard module (python) to control keyboard
first we need install a module name- keyboard in python
pip3 install keyboard
First, let's import the module:
import keyboard
Next, you can also simulate key presses using the send() function:
it will press space:
keyboard.send("space")
This will press and release the space button. In fact, there is an equivalent function press_and_release() that does the same thing.
You can also pass multi-keys:
keyboard.send("windows+d")
The + operator means we press both buttons in the same time, you can also use multi-step hotkeys:
send ALT+F4 in the same time, and then send space,
keyboard.send("alt+F4, space")
But what if you want to press a specific key but you don't want to release it ? Well, press() and release() functions comes into play:
press CTRL button
keyboard.press("ctrl")
release the CTRL button
keyboard.release("ctrl")
So this will press CTRL button and then release it, you can do anything in between, such as sleeping for few seconds, etc.
But now what if you want to write a long text and not just specific buttons ? send() would be inefficient. Luckily for us, write() function does exactly that, it sends artificial keyboard events to the OS simulating the typing of a given text, let's try it out:
keyboard.write("Python Programming is always fun!", delay=0.1)
Setting delay to 0.1 indicates 0.1 seconds to wait between keypresses, this will look fancy like in hacking movies!
THANKS!

pressing keys together in pyautogui to make them make a function?

I have been recently trying to create a program that makes new folder in python(pyautogui).
Here is my code:
import pyautogui;# import the library
pyautogui.press('ctrl');# makes our program to press 'ctrl'
pyautogui.press('n');# makes our program to press 'n'
Apparently what it does instead is pressing ctrl and n individually and I want from them to be pressed together. Please help.
According to the docs:
The press() function is really just a wrapper for the keyDown()
and keyUp() functions, which simulate pressing a key down and then
releasing it up.
As you want to combine several key presses, you need to call keyDown() and keyUp() separately:
pyautogui.keyDown('ctrl')
pyautogui.press('n')
pyautogui.keyUp('ctrl')

Make auto key and mouse press on activate window on Python

I want to write an app in Python on Windows to do some jobs repeatedly.
For example, I need to convert some files into other type. I have a software installed in Windows to do that. However, that program was designed to do it file by file. Now I want to do it automatically.
Therefore, I need to write a software to simulate the key press on active windows. There are a lot of code on autokeyboard but it only works in terminal which run Python script. Specially, after I run Python script, I minimize the terminal, then open some program then the Python script will simulate the key press and/or mouse click in this program.
I found a lot of program can do something like hotkey and after press hotkey, it will simulate some key press and mouse. So I think it is possible.
Could anyone give me a solution for that?
Thanks.
This will help you to automate:
for mouse clicks:
import pyautogui
pyautogui.click(1319, 45)
pyautogui.scroll(200)
pyautogui.hotkey("ctrlleft", "a")
For keyboard
import keyboard
# It writes the keys r, k and endofline
keyboard.press_and_release('shift + r, shift + k, \n')
keyboard.press_and_release('R, K')
# it blocks until esc is pressed
keyboard.wait('esc')
# It records all the keys until escape is pressed
rk = keyboard.record(until='Esc')
# It replay back the all keys
keyboard.play(rk, speed_factor=1)

Code that recognises a keypress in Python 3

Is there anyway for python 3 to recognise a keypress? For example, if the user pressed the up arrow, the program would do one thing whereas if the down arrow was pressed, the program would do something else.
I do not mean the input() function where the user has to press enter after the keypress , I mean where the program recognises the keypress as some as it was pressed.
Is this question too confusing? xD
Python has a keyboard module with many features. You Can Use It In Both Shell and Console.
Install it, perhaps with this command:
pip3 install keyboard
Then use it in code like:
import keyboard #Using module keyboard
while True: #making a loop
try: #used try so that if user pressed other than the given key error will not be shown
if keyboard.is_pressed('up'): #if key 'up' is pressed.You can use right,left,up,down and others
print('You Pressed A Key!')
break #finishing the loop
else:
pass
except:
break #if user pressed other than the given key the loop will break
You can set it to multiple Key Detection:
if keyboard.is_pressed('up') or keyboard.is_pressed('down') or keyboard.is_pressed('left') or keyboard.is_pressed('right'):
#then do this
You Can Also Do Something like:
if keyboard.is_pressed('up') and keyboard.is_pressed('down'):
#then do this
It Also Detect Key For The Whole Windows.
Thanks.
I assume this is a gui program,
If using the built-in gui module Tkinter, you can use bind to connect a function to a keypress.
main.bind('<Up>', userUpkey)
Where userUpKey is a function defined in the current scope.

Categories