Home > Blockchain >  Disable mouse and keyboard using python in windows
Disable mouse and keyboard using python in windows

Time:01-22

I am working with python 3.9 and above to develop UI Automation with mouse click and keyboard click.

I need to freeze or block or hold the mouse click, move, drag and keyboard input when my application working in automation.

I've tried

  • pynput
  • pyHook (only available in 2.7, and I cannot downgrade my python version to 2.7).
  • ctypes

none of this method really working in my case. I am using Windows server as my production environment.

oh, actually i've tried ctypes

from ctypes import *

ok = windll.user32.BlockInput(True)

but I cannot make it to exe with auto-py-to-exe and other tools to convert py to exe.

Thank you.

CodePudding user response:

You can utilize a package made for disabling Windows drivers.

Installation:
pip install devcon-win

Compatibility:
devcon_win is compatible for both python2 and python3.

Using it:

  1. The devcon.exe present in this rep is for '64 bit windows 10'. If your windows's version is different then download devcon.exe as per requirement.

  2. Move devcon.exe in the current script's directory or any path defined in environment variables say Python27/Scripts.

  3. Install devcon_win from pip.

  4. Use devcon_win_driver.py as a sample to disable webcam or any other driver as per requirement {BE CAREFUL}:

Test out the installation by displaying the status of a driver:

# You may need to try a different driver 
# if this doesn't exist on your machine.

import devcon_win

# To display status
print(devcon_win.HP_TrueVision_HD()) 
# Or
print(devcon_win.HP_TrueVision_HD('status')) 

P.S: list of examples of devcon commands are present here.

The rest is really dependant on your machine and the devices attached to it.

CodePudding user response:

You can try using the pywinauto library to automate UI interactions in Windows. This library allows you to simulate mouse and keyboard inputs, as well as manipulate windows and controls. The library works with Windows GUI, so you can use it to interact with any application that has a GUI.

You can install it using pip:

pip install pywinauto

You can then use the mouse and keyboard modules in pywinauto to simulate inputs, for example:

from pywinauto.mouse import move, click

# move the mouse to (100, 100)
move(coords=(100, 100))

# left-click at (100, 100)
click(coords=(100, 100))

Regarding the conversion of the script to .exe, you could use pyinstaller library to convert your script to .exe format.

pip install pyinstaller

Then you can use the command

pyinstaller --onefile script_name.py

to create the exe version of your script.

  • Related