I want to export the following code from a MAC with python 2.7 to a Windows with the following dependency (pynput) which I am importing from pip.
I'd like the person to be able to run the file in their terminal without installing pynput.
I tried converting the file to an executable but it didn't even work on my machine.
Here is the code:
import thread
import random
import time
from pynput.mouse import Listener, Controller, Button
mouse = Controller()
trigger = False
def on_click(x, y, button, pressed):
global trigger
if str(button) == "Button.middle" and pressed:
trigger = True
print "Middle button has been pressed"
if str(button) == "Button.middle" and pressed == False:
print "Middle button has been unpressed"
trigger = False
def loop_thread(threadName, delay):
while True:
time.sleep(delay)
if trigger == True:
sleep_time = random.uniform(0.02, 0.12)x
time.sleep(sleep_time)
mouse.click(Button.left, 1)
def listener_thread(threadName, delay):
with Listener(on_click = on_click) as listener:
listener.join()
try:
thread.start_new_thread( loop_thread, ("Thread-1", 0.025 ) )
thread.start_new_thread( listener_thread, ("Thread-2", 0.25, ) )
except:
print "Error: unable to start thread"
while 1:
pass
Do you know if there's any way to make python scripts cross platform and to include dependencies in said script?
The executable need to be made on the same operating system to work, so no freezing, Cython or nuitka. You could try pex, I think it works across OS, you will have to ship your programm and the pex file.
Related
The following code works perfectly, the loop can be stop by pressing esc:
import time
import keyboard
run = 1
def end():
global run
run = 0
print(run)
def do_stuff():
while run:
print('running')
time.sleep(0.5)
keyboard.add_hotkey('esc', end)
do_stuff()
But if I start this loop with another add_hotkey, I cannot stop it with esc anymore.
import time
import keyboard
run = 1
def end():
global run
run = 0
print(run)
def do_stuff():
while run:
print('running')
time.sleep(0.5)
keyboard.add_hotkey('esc', end)
# do_stuff()
keyboard.add_hotkey('enter', do_stuff)
keyboard.wait()
What should I do to stop this loop? I tried to replace the while run: with while not keyboard.is_pressed('esc'):. It can stop the loop if I hold the esc for a while. But it doesn't seem like a good solution.
=======================
updates:
the following works:
import keyboard
import threading
run = 1
def end():
global run
run = 0
print(run)
def do_stuff():
while run:
print('running')
time.sleep(0.5)
def new_do_stuff():
t = threading.Thread(target=do_stuff, name='LoopThread')
t.start()
keyboard.add_hotkey('esc', end)
keyboard.add_hotkey('enter', new_do_stuff)
keyboard.wait('esc')
Since in the second example you enter the do_stuff() loop through the hotkey and never leave the do_stuff() loop, the system is still captured in the hotkey command and is not listening for hotkeys anymore. You would have to find a way to leave the loop after the keyboard.add_hotkey('enter', do_stuff) command and enter it externally through another way, so the system listens for hotkey-entries again.
I'm not aware of the context you're using this in, but using some sort of a main-loop, that does nothing but wait for a flag to be set (it should be set when you get the hotkey interrupt) and then enters the do_stuff() loop seems like a way to solve it.
I'm trying to create a program that spams your screen with terminal windows using threading and would like to create a failsafe button that force quits python when a certain parameter is met. I've tried "os._exit()" and that does not work. I am looking for a way to destroy all threads and force quit python as if my program runs for too long it will crash my computer.
def spam():
for i in range(30):
if i % 10 == 0: # if i is divisible by 10, it pauses for comedic timing
time.sleep(2)
os.system('open -a Terminal .') # opens terminal window
playsound('/Users/omar/Downloads/boom.wav') # plays comical 'vine' funny meme audio
thread = multiprocessing.Process(target=spam) # creates a thread
thread.start()
if pyautogui.failSafeCheck() == True or pynput.keyboard.Controller().type == 'Keyboard':
os._exit() and thread.terminate()
spam() # function call
My project is to make a program that you can run while you are playing games or other programs in the background.
When you press a certain key, your notepad should open and also close after you press the same key again.
I have managed to open notepad with subprocess and that works fine but I have no idea to make it open only when a certain key is pressed.
Thanks for any help!
EDIT:
What I tried already:
import subprocess
import keyboard
if keyboard.is_pressed('k'):
subprocess.Popen('C:\\Windows\\System32\\notepad.exe')
input()
here it just doesn't detect any keyboard input, the input() at the end makes the program not close instantly
import subprocess
import keyboard
keyboard.add_hotkey('ctrl+k', print,args=("hello", "test"))
input()
Here if I press "ctrl+k it" will print hello test that means the hotkey works fine. When I switch this part "print,args=("hello", "test")" to "subprocess.Popen('C:\Windows\System32\notepad.exe')"(it should open the program instead of printing hello test) the notepad opens instantly after I run the program and when I press "ctrl+k" I get a big error.
A more complex, but still working example could be the following. With this code your program will be always listening the keyboard, not only when you are focused on the input, so may be mre practical in your case
from pynput import keyboard
import subprocess
import threading
class MyException(Exception): pass
class Listening:
"""Is allways waiting for the keyboard input"""
def __init__(self):
self.notepad_open = False # to know the state
with keyboard.Listener(
on_press=self.on_press) as listener:
try:
listener.join()
except:
pass
def on_press(self, key):
try:
if key.char == "k":
if not self.notepad_open:
self.subprocess = \
subprocess.Popen('C:\\Windows\\System32\\notepad.exe')
self.notepad_open = True # update state
else:
self.subprocess.kill()
self.notepad_open = False # update state
except: # special key was pressed
pass
thread = threading.Thread(target=lambda: Listening())
thread.start()
The problem is that you check for the key 'k' only once at the beginning. If you want the program to correctly work then you should try this:
import time
import subprocess
import keyboard
while True:
if keyboard.is_pressed('k'):
subprocess.Popen('C:\\Windows\\System32\\notepad.exe')
time.sleep(5)
-I used the time so that you can only open the program once 5 seconds(If you're curious, see what happens without it)-
I'm writing a Python script using Pygame and OSC that will allow me to control the music tracker software Renoise (version 3.0.0) by doing things in a Pygame (1.9.1) environment and sending messages to the software via OSC. If you're wondering, the purpose of this script is for live performance and generative music creation. I'm having a pretty annoying issue, however: when I try to close the Pygame window, it just hangs - doesn't even respond to Control+C - but this only happens if Renoise is open, or has been opened at least once during the session. If I reboot and run the script without opening Renoise, it works as intended. What could possibly be the issue here?
I'm using Python version 2.7.6, Pygame version 1.9.1, and Renoise version 3.0.0. This is all running on an Xubuntu 14.04 laptop.
Code for my main script (the imported OSC module is simply OSC.py available in many places on the internet):
#!/usr/bin/python
import sys
import pygame
import OSC
from pygame.locals import *
class Pot:
def __init__(self, ip, port):
self.clk = pygame.time.Clock()
self.screen = pygame.display.set_mode( (1600, 900) )
self.fillColor = pygame.Color(255,255,255)
pygame.display.set_caption("Pygame OSC Test")
self.client = OSC.OSCClient()
self.client.connect( (ip, port) )
def oscsend(self, addr, *data):
msg = OSC.OSCMessage()
msg.setAddress("/renoise" + str(addr))
for d in data:
msg.append(d)
try:
self.client.send(msg)
except OSC.OSCClientError, err:
print err
def tick(self):
# clear the screen
self.screen.fill(self.fillColor)
#TODO: other drawing stuff here
# process events
for event in pygame.event.get():
if event.type == QUIT:
self.client.close()
del(self.client)
return False
pygame.display.update()
self.clk.tick(10)
return True
if __name__ == "__main__":
p,f = pygame.init()
print "Num modules passed:", p
print "Num modules failed:", f
pot = Pot("localhost", 8000)
running = True
while running:
running = pot.tick()
pygame.quit()
sys.exit(0)
I managed to fix my problem by making sure pygame.mixer wasn't initialized. I assume it was causing some problems with Renoise by them both using the sound hardware at the same time.
To only initialize certain modules of pygame, just call their initialization methods individually. For instance, pygame.display.init() will initialize the Display module. Calling pygame.init() is simply a shortcut to initializing all the currently loaded modules. Hope this helps somebody!
Edited version after a closer look: You are returning False when the programme is quitted. But the variable "running" still remains true! Your loop is still going about his business and happily looping there...
I'd change it this way:
for event in pygame.event.get():
if event.type == QUIT:
self.client.close()
del(self.client)
pygame.quit()
sys.exit()
2nd option:
return False
Change this part that "running" is set to false. i'd go with the first option though.
If that doesn't solve the problem, please let me know, that would be an interesting issue to have a closer look at.
Is there a way of disabling or locking mouse and keyboard using python? I want to freeze the mouse and disable the keyboard.
I haven't tested (actually I've tested the mouse part, and it annoyingly works) but something like this using pyhook would do what you want:
import pythoncom, pyHook
def uMad(event):
return False
hm = pyHook.HookManager()
hm.MouseAll = uMad
hm.KeyAll = uMad
hm.HookMouse()
hm.HookKeyboard()
pythoncom.PumpMessages()
I have extended Fábio Diniz's answer to a class which provides both a block() and an unblock() function which block (selectively) mouse/keyboard inputs. I also added a timeout functionality which (hopefully) addresses the annoyance of locking oneself out.
import pyHook
from threading import Timer
import win32gui
import logging
class blockInput():
def OnKeyboardEvent(self,event):
return False
def OnMouseEvent(self,event):
return False
def unblock(self):
logging.info(" -- Unblock!")
if self.t.is_alive():
self.t.cancel()
try: self.hm.UnhookKeyboard()
except: pass
try: self.hm.UnhookMouse()
except: pass
def block(self, timeout = 10, keyboard = True, mouse = True):
self.t = Timer(timeout, self.unblock)
self.t.start()
logging.info(" -- Block!")
if mouse:
self.hm.MouseAll = self.OnMouseEvent
self.hm.HookMouse()
if keyboard:
self.hm.KeyAll = self.OnKeyboardEvent
self.hm.HookKeyboard()
win32gui.PumpWaitingMessages()
def __init__(self):
self.hm = pyHook.HookManager()
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
block = blockInput()
block.block()
import time
t0 = time.time()
while time.time() - t0 < 10:
time.sleep(1)
print(time.time() - t0)
block.unblock()
logging.info("Done.")
You can have a look at the main routine for example usage.
For me, just two lines of programming solved the problem:
from ctypes import *
ok = windll.user32.BlockInput(True) #enable block
#or
ok = windll.user32.BlockInput(False) #disable block
Totally different take since all the solutions mentioned above use a quiet outdated library(pyhook) and this pyhook method personally didnt work for me.
import keyboard
from pynput.mouse import Controller
from time import sleep
def blockinput():
global block_input_flag
block_input_flag = 1
t1 = threading.Thread(target=blockinput_start)
t1.start()
print("[SUCCESS] Input blocked!")
def unblockinput():
blockinput_stop()
print("[SUCCESS] Input unblocked!")
def blockinput_start():
mouse = Controller()
global block_input_flag
for i in range(150):
keyboard.block_key(i)
while block_input_flag == 1:
mouse.position = (0, 0)
def blockinput_stop():
global block_input_flag
for i in range(150):
keyboard.unblock_key(i)
block_input_flag = 0
blockinput()
print("now blocking")
sleep(5)
print("now unblocking")
I just slightly modified the #Robert code and instead of the time I used external interrupt to close the program i.e. if you connect any external drive then the program gets close and your mouse and keyboard will be working perfectly.
import pyHook
from threading import Timer
import win32gui
import logging
import win32file
def locate_usb():#this will check any external Drives
drive_list = []
drivebits = win32file.GetLogicalDrives()
# print(drivebits)
for d in range(1, 26):
mask = 1 << d
if drivebits & mask:
# here if the drive is at least there
drname = '%c:\\' % chr(ord('A') + d)
t = win32file.GetDriveType(drname)
if t == win32file.DRIVE_REMOVABLE:
drive_list.append(drname)
return drive_list
class blockInput():
def OnKeyboardEvent(self,event):
return False
def OnMouseEvent(self,event):
return False
def unblock(self):
try: self.hm.UnhookKeyboard()
except: pass
try: self.hm.UnhookMouse()
except: pass
def block(self ,keyboard = True, mouse = True):
while(1):
if mouse:
self.hm.MouseAll = self.OnMouseEvent
self.hm.HookMouse()
if keyboard:
self.hm.KeyAll = self.OnKeyboardEvent
self.hm.HookKeyboard()
win32gui.PumpWaitingMessages()
cg= locate_usb()
if cg:
break
def __init__(self):
self.hm = pyHook.HookManager()
if __name__ == '__main__':
block = blockInput()
block.block()
block.unblock()
I hope this code will help you
Since 2018, you can now use pynput (v1.4+) to suppress keyboard and mouse events on Windows, Mac, and Linux.
import pynput
# Disable mouse and keyboard events
mouse_listener = pynput.mouse.Listener(suppress=True)
mouse_listener.start()
keyboard_listener = pynput.keyboard.Listener(suppress=True)
keyboard_listener.start()
# Enable mouse and keyboard events
mouse_listener.stop()
keyboard_listener.stop()
This also disables mouse scrolling and clicking.
However, this does not stop users from pressing Ctrl+Alt+Del on Windows. But you can run the script in an elevated command prompt, and the mouse and keyboard will still be disabled, even if they opened Task Manager using Ctrl+Alt+Delete, so there is no harm done (apparently there are way to actually prevent Ctrl+Alt+Delete, but do your own research for that)
You can use pyautogui to do this. Though I recommend adding keyboard for making a stopping key. First, you want to install pyautogui and keyboard.
Please note: this only disables the mouse not the keyboard, that is a very bad idea.
pip install pyautogui
pip install keyboard
Ok, with that sorted, we have to actually make the disabler.
import pyautogui
import keyboard
stopKey = "s" #The stopKey is the button to press to stop. you can also do a shortcut like ctrl+s
maxX, maxY = pyautogui.size() #get max size of screen
While True:
if keyboard.is_pressed(stopKey):
break
else:
pyautogui.moveTo(maxX/2, maxY/2) #move the mouse to the center of the screen
Ok, but there is 2 ways to get out of this. pressing S, and also quickly moving the mouse to one of the corners of the screen (that is a pyautogui failsafe, but we can disable that). If you want to disable the failsafe, add this after the imports:
pyautogui.FAILSAFE = False
Please note that disabling the failsafe is NOT recommended!
Ok, so now the only way to exit is the S key. If you want to stop this somewhere else in your program, do this:
pyautogui.press(stopKey)
Ok, so its not perfect, but it will stop you from doing basically anything with your mouse.