Not able to select text in pynput - python

I'm writing a python script using pynput library, in which I want to select the text of particular length.
For that I'm first pressing shift and then pressing multiple times left arrow key. So that the text left to the cursor id selected and can be further used for copying purpose.
Here is my code:
from pynput import keyboard
from pynput.keyboard import Controller
keyboard_controller = Controller()
with keyboard_controller.pressed(keyboard.Key.shift_l):
keyboard_controller.press(keyboard.Key.left)
keyboard_controller.release(keyboard.Key.left)
keyboard_controller.press(keyboard.Key.left)
keyboard_controller.release(keyboard.Key.left)
keyboard_controller.press(keyboard.Key.left)
keyboard_controller.release(keyboard.Key.left)
This moves my cursor to left by 3 steps but even if the shift is pressed, the text is not getting selected. What I'm missing here?
Edit:
OS: Windows 10
Environment: Python 3.6.5
Here is demo of what is happening in my vs code.

Maybe try this?
from pynput.keyboard import Key
from pynput.keyboard import Controller
keyboard = Controller()
Option 1:
with keyboard.pressed(Key.shift):
keyboard.press(Key.left)
keyboard.release(Key.left)
keyboard.press(Key.left)
keyboard.release(Key.left)
Option 2:
keyboard.press(Key.shift)
keyboard.press(Key.left)
keyboard.release(Key.left)
keyboard.press(Key.left)
keyboard.release(Key.left)
keyboard.release(Key.shift)
Option 3:
keyboard.press(Key.shift.value)
keyboard.press(Key.left.value)
keyboard.release(Key.left.value)
keyboard.press(Key.left.value)
keyboard.release(Key.left.value)
keyboard.release(Key.shift.value)
Here is video proof it works on my machine :)

I've just toyed around with pynput and pyautogui and finally found a combination using pynput that will complete this shift hotkey function. I was using page_down rather than arrow keys for my code but I confirmed that both work. For some reason if you press down both the shift key and shift_r key, you are able to highlight text by moving the cursor.
Setup:
from pynput.keyboard import Key, Controller
keyboard = Controller()
Code:
keyboard.press(Key.shift)
keyboard.press(Key.shift_r)
keyboard.press(Key.page_down) # or keyboard.press(Key.left)
keyboard.release(Key.page_down) # or keyboard.release(Key.left)
keyboard.release(Key.shift_r)
keyboard.release(Key.shift)

Related

Is there a way to edit the maximum CPS in pynput?

So recently I've been using pynput and I have noticed that no matter the delay I set it as the maximum CPS with pynput is always 10 cps. Now in pyautogui you can edit the hard coded pause between clicks, but I was wondering if this is possible in pynput.
For example:
import pynput
from pynput.mouse import Button as btn
from pynput.mouse import Controller
import time
mouse = Controller()
delay = 0
mouse.click(btn.left)
time.sleep(delay)
No matter what you change the "delay" value to, the max CPS is always 10 with Pynput, and trying "pynput.PAUSE = 0.01" doesn't change the output.

Double click when clicking once pynput

Trying to do a program that double clicks for you when you click the left mouse button once with pynput. I have the following code, but if I run the code, my mouse glitches out and stops working.
from pynput.mouse import Listener, Button, Controller
mouse = Controller()
def on_click(x, y, button, pressed):
if pressed == True:
mouse.click(Button.left, 2)
else:
pass
with Listener(on_click=on_click) as listener:
listener.join()
Also in addition of this, how would the implementation of pressing "F10" enables that 1 click acts like double click and pressing "F10" again would disable it, so 1 click would act like 1 click be possible?
Oh,I maybe find your problem,
Two probable causes:
In your script,when your press the mouse button.It will call function on_click.Then it will mouse.click(Button.left, 2).But this code will also call on_click.So it will be a endless loop.Finally,you will find your mouse will be not responding.So I think you should use another way to do that.
In pynput official document,It seems it can be used in macOS(maybe windows couldn't use it.And I also found if I only use mouse.click(Button.left, 2) in my PC,my python will be not responding.(It couldn't be stopped).Maybe you should just use .press and .release directly):
Also in addition of this, how would the implementation of pressing "F10" enables that 1 click acts like double click and pressing "F10" again would disable it.
So this seems a switch,you can use a global variable to do that.There is a minimal example of use pynput to do a switch.(This will don't print Mode is on if you don't press F10,and it won't print it after you press F10 again).
# import win32api,win32con
from pynput.mouse import Controller
from pynput import keyboard
from pynput.keyboard import Key
mouse = Controller()
Mode = False
def on_press(key):
global Mode
if key == Key.f10:
if Mode:
Mode = False
else:
Mode = True
listener = keyboard.Listener(on_press=on_press)
listener.start()
while True:
if Mode:
print("Mode is on")

Binding a Keyboard Key to a Function - Python KeyListener

"You've pressed the Enter Key!"
Whenever I press Key(z) the function should be executed:
#Pseudocode:
bind(<Enter>, function_x)
I'm currently working on a python program, which will run in a constant loop. It runs only on the console (no GUI), but still I need to be able to interact with the program at any time without having the program asking for input.
Several Modules solve this Problem
Pynput
(pip install pynput)
Simple module for handling and controlling general inputs
from pynput import keyboard
from pynput.keyboard import Key
def on_press(key):
#handle pressed keys
pass
def on_release(key):
#handle released keys
if(key==Key.enter):
function_x()
with keyboard.Listener(on_press=on_press,on_release=on_release) as listener:
listener.join()
(See pynput docs)
Keyboard (pip install keyboard)
A simple module for simulating and handling keyboard input
keyboard.add_hotkey('enter', lambda: function_x())
(See Keyboard docs)
Tkinter
Integrated UI Module, can track inputs on focused thread
from tkinter import Tk
root = Tk() #also works on other TK widgets
root.bind("<Enter>", function_x)
root.mainloop()
Be aware: These solutions all use Threading in some way. You might not be able to execute other code after you've started listening for keys.
Helpful threads:
KeyListeners, Binding in Tkinter
feel free to add more solutions
No beans with module keyboard.
Simply way to clear screen:
print('\033[2J') # clear screen, but stay current cursor line
print('\033[H') # move cursor 1,1 home position
or
print('\033[H\033[2J') # one step to clear screen and move at home position
You may define function like:
def cls():
print('\033[H\033[2J')
It works with tedious call braces.
I like to bind previous functionality to keyboard 'scroll lock' key, but how?

python keyboard.press not working with other applicatons

I have made a python script that presses space once a second so I can go AFK in games and not get kicked for being idle. I tested it in notepad and it works but when i open Minecraft or Roblox nothing happens.
I did try looking for an answer but couldn't find anything
from pynput.keyboard import Key, Controller
import time
keyboard = Controller()
while True:
time.sleep(1)
keyboard.press(' ')
keyboard.release(' ')
Sometimes games won't recognize a super-short key press. You essentially have a space press of less than a microseconds. Try adding a delay between the press and release.
from pynput.keyboard import Key, Controller
import time
keyboard = Controller()
while True:
time.sleep(1)
keyboard.press(' ')
time.sleep(0.15)
keyboard.release(' ')
you should probably run the cmd as administrator and put in some delay for keys to be registered. play around with the values of the delay to find one that suits your need
from pynput.keyboard import Key, Controller
import time
import keyboard
keyboard = Controller()
while 1:
if keyboard.is_pressed('r'):
while True:
time.sleep(1)
keyboard.press('space')

python3: how to use the press Ctrl+X (cut) and Ctrl+V using pynput?

following pynput documentation I tried this to "cut":
1: select some text in an editor
2: run this_code.py using a shortcut (without leaving the active windows)
from pynput.keyboard import Key, Controller
keyboard = Controller()
with keyboard.pressed(Key.ctrl):
keyboard.press('x')
keyboard.release('x')
The python console open actually print: ^X. The combination of keys are right but it doesn't do what it's suppose to do: cut the selected text store it in the clipboard. (I'm not interested to just store the clipboard content in a variable, I want a Ctrl+C)
I guess this answser will also solve the remaining part: Ctrl+V (to past some data which will be first inserted in the clipboard)
I took 3 things into consideration:
since I am on a mac, the combination is Command+X instead of Ctrl+X
I can only make it work if I use keyboard.press (pressed doesn't work for me, don't know why)
For the special keys, I have to use their Key.value (so, Key.ctrl becomes Key.ctrl.value; Key.Shift becomes Key.Shift.value...)
In the end, this worked for me:
# I tested this code as it is in Mac OS
from pynput.keyboard import Key, Controller
keyboard = Controller()
# keyboard.press(Key.ctrl.value) #this would be for your key combination
keyboard.press(Key.cmd.value)
keyboard.press('x')
keyboard.release('x')
# keyboard.release(Key.ctrl.value) #this would be for your key combination
keyboard.release(Key.cmd.value)
Even though this question is a bit old, I was having this same problem and found a solution that worked for me. Might come in handy for someone in the future.
I think you should use keyboard.type(msg) instead of keyboard.press(key)
I tried it on my side, and I think the problem is that you are using Key.ctrl as the argument for keyboard.pressed() on line 3.
Instead of that you should be using Key.ctrl.value.
So, the correct version of the code you have given would be:
from pynput.keyboard import Key, Controller
keyboard = Controller()
with keyboard.pressed(Key.ctrl.value):
keyboard.press('x')
keyboard.release('x')
First install pyautogui with:
pip install pyautogui
#Then in your code write:
import pyautogui
pyautogui.hotkey('ctrl', 'v')

Categories