Automate to click 'down' key and take screenshot - python

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!

Related

Can I put input in Mindstorms 51515 while programming in python?

When I was programming in Mindstorms Robot Inventor, and decided to put an input in my code, I bumped into this NameErrorThis is the Error
Is it Even possible to put an input in Mindstorms??
The Python available isn’t the complete Python standard library. The terminal view in the code editor doesn’t provide for a way to put in input either, so unfortunately no.
What you can do instead is use the buttons on the hub as a type of input. This wouldn’t be free-form text, but if you’re using input() as a user-controlled wait before progressing, you can do that.
_ = hub.right_button.was_pressed() # clear out any button press
while not hub.right_button.was_pressed():
# do something
# do something after the button was pressed
You can string multiple of the above in a row if you want to do multiple actions separated by waiting for you to let it continue, or even take different actions based on which button was pressed.
Finally, you could do a very slow text input using the buttons and the display screen to scroll through the alphabet, confirm the letter, and finish the input.

Can't move minecraft character using pynput

I'm trying to use Pynput to move my Minecraft character, but when I press run, it finishes without moving my character. I've retested it on stuff like the Minecraft chat and it works fine, I've also tested it on the Google Chrome search and it works, but weirdly not in Minecraft. My code is very simple.
from pynput.keyboard import Key, Controller
import time
myKeyboard = Controller()
time.sleep(3)
myKeyboard.press('w')
myKeyboard.release('w')
time.sleep(3)
myKeyboard.press('s')
myKeyboard.release('s')
I don't see anything wrong with my code and I don't know if pynput even works with moving Minecraft characters, or any characters for that matter.
According to pynput documentation, keeping a button pressed is best used with the pressed method and a with statement, like this:
with myKeyboard.pressed('w'):
time.sleep(3)
It should in theory hold the w key pressed for 3 seconds.

Does Python evdev library have an event specific grab or passthrough?

A python evdev device has a .grab() function that prevents other processes from getting input events on the device. Is there any way to limit this to specific events from a device?
For my example, if I .grab() a pen input device that has pressure sensitivity and tilt and 2 click buttons on the side, how would I 'grab' ONLY the 2 click buttons but let the rest of the input (the tip, pressure sensitivity and tilt) be caught by the rest of the system as normal?
One of my pen buttons is normally a right click mouse event. I want to make it do something else but it still pops up the right click menu so I'm trying to figure out how to stop that.
I tried doing the grab and ungrab when the event occurs. Like event > grab > do my stuff > ungrab. But that is obviously too late and the OS still pops up the menu.
I tried doing the full grab, then in the event loop if it is a button press do my stuff, otherwise create a UInput event injection by just passing the event back to the system. This was a bit of a tangled mess. Permissions are required. When I finally got past that, the movement was offset and the pressure/tilt wasn't working... I think it is something to do with the DigiMend driver that actually makes that stuff work and/or xinput settings I have to pass to calibrate the tablet. But I'm not interested in writing all the pressure/tilt functionality from scratch or anything like that, so I need the DigiMend stuff to work as normal. So I gave up on this idea for now.
The only other thought I had was figure out why the OS defaults to the behavior it does and see if I can just manually disable the actions (i.e. Why does it think that button is a right mouse click and make it think that button is nothing instead.)
So I guess this is a 3 level question.
Can I achieve the the grab functionality on select events instead of the device as a whole?
If the passthrough idea was better, is there a way to achieve this without having to do any permission modifications and be able to pass the exact event (i.e. no offset and such that I experienced?)
If evdev does not have this ability or it'd be easier to do in another way, like disabling the defaults for the pen in the OS somehow, I am open to suggestions. I am using Kubuntu 20.04 if that helps.
Any help would be appreciated, let me know if more info is needed, thanks in advance!
I ended up going with #3 and using xinput. Figured I'd put up this answer for now in case others come across this and want to do something similar.
The workaround was actually kind of simple. I just use xinput to remap the 2 buttons. So evdev doesn't have to grab at all. Just disable those buttons and everything goes normally except those, which I listen for with evdev.
xinput set-button-map {} 1 0 0 4 5 6 7
My device has 7 buttons and are normally mapped 1-7. Which are all mouse equivalents of left click, middle click, right click, etc...
By using that string and passing the device ID in for the {} I just run that with subprocess first. And voila, no more right click menu. And I can use evdev to map the events to whatever I want.

Shift and mouse click

I am trying to hold shift key and at the same time click with the mouse. I searched the internet and found a stackoverflow question about this. This is the post: Pyautogui - Need to hold shift and click
Also the code in this post was working for like three times!
Then suddenly it stopped working. It is really strange. I also tried it with pynput. Here is my post: Holding shift key + mouse click
It seems like holding shift and clicking the mouse are working seperately.
However, together it seems not to work
This is the code:
import pyautogui
import time
time.sleep(2)
pyautogui.keyDown('shift')
pyautogui.click()
pyautogui.keyUp('shift')
I am running on windows OS
It looks working fine?
Maybe use more keydown('shift') will make it
I will suggest you not to use pyautogui.
Its easy and simple with other modules. Install keyboard(for controlling keyboard) and mouse(for controlling mouse)
Here is a sample code that does what you want:
import keyboard, mouse #< Importing the required modules
keyboard.press("shift") #< Presses and holds the key
mouse.click("left") #< Makes Left click
keyboard.release("shift") #< Releases the held key

Python simulating 'enter' keypress in a game

So I've been attempting to get a script to type out a string of text in a video game (Guild Wars 2). Mainly I'm using pyautogui and for the most part it works fine. My issue is it seems I can't get the game to recognize 'enter'. For example if I have the code:
import pyautogui, time
time.sleep(2) #to allow me to have time to switch windows
pyautogui.press('enter')
pyautogui.typewrite("This is a test")
pyautogui.press('enter')
The two "press enter" function will not open and submit the text. If however I manually hit enter, the 3rd line types things out just fine.
I've also tried replacing press('enter') with keyDown followed by keyUp, but with still no results.
I've managed to create a workaround by having python hit F10, and then a separate Autohotkey script hitting enter when F10 is hit, but that is far from ideal. Are there any suggestions?
Extra note from comments: The script by itself works fine in other programs such as notepad. It seems to fail exclusively for the game client.
For anyone finding their way to this post. The answer is essentially that this can't be done in this fashion due to how most games interpret keypresses. A working system is shown here: Simulate Python keypresses for controlling a game
None of \n nor return works to me, so I have to use pydirectinput: https://github.com/learncodebygaming/pydirectinput
Install it using pip install pydirectinput, then import it and use as the README. Note that the file also needs to run as admin.

Categories

Resources