My laptop keyboard has a bug and it sometimes presses the number 5 randomly so i tried many things and they didn't work, I tried programming a code that can stop it but i couldn't because i am a beginner.
from pynput.keyboard import Key, Controller
keyboard = Controller()
disable = keyboard.pressed('5')
if deni:
KeyboardInterrupt
continue
hopefully this doesn't need imports
This question does not belong here. You should have put it in https://superuser.com/ as it would have been a whole lot easier and more effective to do it in some other programming language. But if you want to do it in python, first learn python.
Other Options
Try pressing "Fn and Num Lock" or if you don't have "Num Lock" do "Fn and Caps Lock.
Try reinstalling your keyboard driver.
If your keyboard has gotten water inside of it, then put it in rice and let it sit for 2 to 3 days
Buy a new keyboard
Resources
http://www.tomshardware.com/answers/id-1972361/keyboard-keys-typing-wrong-multiple-letters.html
Related
sorry in advance for my bad english.
I'm writing a game automation program , specifically the game requires a key press to start doing (something) , but when i use KeyBoard.press() function from KeyBoard module i have problem where game doesn't recognize my key i tried in game chat box and it still works , it means the key is still pressed down but the game doesn't pick up , i tried using some other functions like " KeyBoard.press_and_release() , KeyBoard.send() " and some other modules but the result is the same,
here is the example code
While True:
if (...):
keyboard.press('e')
keyboard.release('e')
but what confuses me is when i add time.sleep() between the press and release commands it works but what confuses me is when i add time.sleep() between the press and release commands it works but there is a big delay
Anyone have any solution for me?
While True:
if (...):
keyboard.press('e')
time.sleep(0.05)
keyboard.release('e')
If I understand properly (and, I might not, sorry) it could be an issue with the game rather than python. Some games, especially online games, implement features specifically to stop automation.
Have you tried your code in a different context - another game or, seeing as it simulates key presses, a text editor? I think you are saying it works in the chat box but not for game control inputs. If it works as intended outside of the game but not inside that would suggest the problem is something to do with how the game handles key presses.
In that while loop, you might be simulating 20 key presses a second, while a more usual average would be between about 5 and 7 with the world record reported at 16. Perhaps the game detects if the rate is too high and discounts those key presses. Also, a physical keyboard will be subject to repeat rate and repeat delay, which the game may be testing to specifically try and eliminate this type of virtualized automation. Or, something else entirely - without knowing which game you are trying to automate I am left with guess work.
That said, with your reference to an in-game chat box, I strongly suspect you are trying to automate an online multi-player game. Even if you are just doing it for your own personal interest (and not to get an advantage over other players), that kind of thing is frowned upon and can potentially get your game account banned. I would normally suggest you share which game you are trying to automate with the community to get more specific information, but I doubt you will be happy to do that.
I've recently realized that Python's pynput module's key click doesn't get registered in certain software. However, physical key clicks work perfectly fine. This raises the question of how are the two different methods been recognized. I thought this would've been an answered question from a long time ago but I can't seem to find any post about this.
Let's take a random game such as Genshin for example. The "w" key on the keyboard would result in the character in the game moving forward. However, a program such as the following that "holds" down the "w" key doesn't get registered by the game.
from pynput.keyboard import Key, Controller
keyboard = Controller()
while True:
keyboard.press("w")
keyboard.release("w")
The two big questions that I have are:
What is the difference between physical key clicks and the program generate key clicks that allowed these softwares to distinguish which method is used?
Based on the answer to the first question, is there a way to bypass such a difference without tempering with the software's code (aka disguise program generated key clicks as physical key clicks)?
Thanks in advance!
Edit: If anyone has any idea on how this works, please let me know. Any help would be much appreciated!
I also encountered this problem when I was trying to write a small program to help myself in Genshin. After an excruciating search, I found these two questions: question1, question2
I'm not very sure how it exactly works, so I can only give you my conclusion: When we run Genshin, we give it admin rights. Programs without admin rights sort of can't interact with it.
To solve this problem, you can merge this into your code:
import ctypes, sys
def is_admin():
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
if is_admin():
# Code of your program here
else:
# Re-run the program with admin rights
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1)
Then you need to make the code an executable with pyinstaller module. When you execute it, it will re-run and ask you for admin rights, then it can do its job on Genshin or other program.
There are certainly games that intentionally try to prevent the kind of cheating you're trying to do. How they do so depends on the operating system.
pynput on Windows, for example, uses the send_input API to inject keystrokes into the input system, but a sufficiently motivated application could be reading at a lower level, by using a keyboard filter driver to pull keystrokes before they get into the input system.
With such a design, there's nothing you can do in user mode to cheat the keys. You would need your own kernel driver, and even then it's not trivial to guarantee the order that the filters load.
I am newbie who wants to learn python. English is not my primary language, please ignore grammer error.
Here's my question.
In tradingview charts, i want to take screenshot, click down button, take screenshot again, wait for 3 second then go to next stock.
As i am newbie on python as well as stackoverflow if my any action fell dumb or wrong then please give me guidence.
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!
Saving your problem
note- in windows 10, hot key for tacking screen shot is "windows+PrtScn.
Use your own hot key
from selenium import webdriver
import keyboard
import time
driver = webdriver.Chrome(executable_path="C:\\Users\\hp\\Desktop\\webcontrol\\chromedriver.exe")
driver.get("url ypu want to reach")
keyboard.send("windows+PrtScn")
keyboard.send("down")
keyboard.send("windows+PrtScn")
time.sleep(3)
driver.get("next stock url you want")
note- for going to next stock you can also use buttons or you can use selenium to click that particular stock
THANKS!
I got my script to work with typing out keys, but for some reason, those keys do not work in Minecraft. Although, typing Minecraft messages in the chat are no problem. I'm trying to get python to move my own character.
In my script, I tried to use pyautogui, the mouse in Minecraft works but not keys such as w, a, s, or d. Is there another import or way to hold down a key that actually works in Minecraft?
def hold_W (hold_time):
start = time.time()
while time.time() - start < hold_time:
pyautogui.press('w')
Doing this and calling it worked for writing texts, but for some reason does not register in Minecraft? Any help?
If you used "Automate the Boring Stuff With Python" to learn this stuff, the book states that pyautogui cannot be used on video games on Windows. I am not sure how to fix this, but the link to the book is here: https://automatetheboringstuff.com/2e/chapter20/
Also, you can go to the documentation of pyautogui here: https://pyautogui.readthedocs.io/en/latest/ to see if the games feature has been added. My greatest regrets that I cannot solve this problem for you. If it makes you feel better, pyautogui does work on .io games.
Different video games have different ways of detecting keystrokes, so to find a thing that will work on all video games you might need to get your hands dirty and do some stuff I have no clue how to do.
As it sounds I wanted to create my own kinda smart assistant that can actually unlock my pc (Cortana can't and I could not find any good solution for this).
for now, I have tried multiple ways including messing with winlogon.exe and MSGINA.dll. After about 3 hours of search, I haven't found anything that could actually unlock my pc, and I saw some programs that said that they have succeeded in unlocking their pc while messing around with this files but in reality, it just prevents the locking mechanism and it is not my intention.
right now I have seen some videos about using pyautogui and keyboard to control keyboard and mouse movement and I wrote a small script that can "unfold" the windows lock screen cover (where the time and date is displayed) but nothing more than that (it is not working at all)
import keyboard
import time
for i in range(10):
print 10 - i
time.sleep(1)
keyboard.send('enter')
time.sleep(2)
keyboard.write("password1")
keyboard.send("enter")
also tried keyboard.press_and_release and it doesn't do anything.
how can I send any keyboard input to the lock screen?
and if it is not possible, do you have any other suggestions?
EDIT: I am Working on windows 10 build 1803 and python 2.7