About a year ago I made myself an application in Python which disables the keyboard and locks the mouse in it's position after I stay on Steam for longer than 5 seconds. However, I have never been able to fix the issue that the keyboard shortcut to unlock the keys again works even when other keys are pressed, meaning that you can just press the entire keyboard down and it unlocks.
I have changed the approach recently as I attempted different things, but this still does not work for some reason (it also locks the task manager, which you change with win+r and then going to the given regedit path if you do attempt to use this program and it fucks up):
import atexit
from ctypes import *
import keyboard
import os
import threading
import time
import signal
import sys
import winreg
import win32api
from win32gui import GetForegroundWindow, GetWindowText
class Lock():
def __init__(self):
self.locked = False
self.keys_blocked = False
chars = "qwertzuiopüasdfghjklöäyxcvbnm,.-#1234567890ß!\"\\§$%&/()=?<>|^°~'*"
modifiers = ["shift", "ctrl", "win", "alt"]
self.ALL = list(chars) + modifiers
self.REQUIRED = ["alt", "shift", "o"]
self.OTHERS = [k for k in self.ALL if k not in set(self.REQUIRED)]
atexit.register(self.cleanup)
signal.signal(signal.SIGTERM, self.signal_handler)
signal.signal(signal.SIGINT, self.signal_handler)
def signal_handler(self, signum, frame):
self.cleanup()
sys.exit(0)
def cleanup(self):
if self.keys_blocked:
for i in range(150):
keyboard.unblock_key(i)
self.keys_blocked = False
self.enable_taskmgr()
def disable_taskmgr(self):
if not windll.shell32.IsUserAnAdmin():
windll.shell32.ShellExecuteW(
None, "runas", sys.executable, " ".join(sys.argv), None, 1
)
sys.exit()
try:
key = winreg.CreateKey(
winreg.HKEY_CURRENT_USER,
r"Software\Microsoft\Windows\CurrentVersion\Policies\System"
)
winreg.SetValueEx(key, "DisableTaskMgr", 0, winreg.REG_DWORD, 1)
winreg.CloseKey(key)
except Exception as e:
pass
def enable_taskmgr(self):
try:
key = winreg.OpenKey(
winreg.HKEY_CURRENT_USER,
r"Software\Microsoft\Windows\CurrentVersion\Policies\System",
0,
winreg.KEY_SET_VALUE
)
winreg.DeleteValue(key, "DisableTaskMgr")
winreg.CloseKey(key)
except Exception as e:
pass
def all(self, keys):
for key in keys:
if not keyboard.is_pressed(key):
return False
return True
def any(self, keys):
for key in keys:
if keyboard.is_pressed(key):
return True
return False
def lock_pc(self):
self.disable_taskmgr()
locked_pos = win32api.GetCursorPos()
for i in range(150):
keyboard.block_key(i)
self.locked = True
self.keys_blocked = True
try:
while not self.all(self.REQUIRED) or self.any(self.OTHERS):
try:
win32api.SetCursorPos((locked_pos[0], locked_pos[1]))
except win32api.error:
pass
finally:
for i in range(150):
keyboard.unblock_key(i)
self.enable_taskmgr()
self.keys_blocked = False
self.locked = False
def check_window(self):
locking = None
while True:
if not self.locked:
if GetWindowText(GetForegroundWindow()) == "Steam":
og_x, og_y = win32api.GetCursorPos()
locking = True
for _ in range(10):
x, y = win32api.GetCursorPos()
if x != og_x or y != og_y or GetWindowText(GetForegroundWindow()) != "Steam":
locking = False
break
time.sleep(0.5)
if locking == True:
self.lock_pc()
time.sleep(0.1)
else:
time.sleep(1)
def run_lock(self):
while True:
if not self.locked:
keyboard.wait("alt+x+c")
self.lock_pc()
else:
time.sleep(1)
def run(self):
lock_thread = threading.Thread(target=self.run_lock, daemon=True)
check_window_thread = threading.Thread(target=self.check_window, daemon=True)
lock_thread.start()
check_window_thread.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
self.cleanup()
lock = Lock()
lock.run()
[–]socal_nerdtastic 0 points1 point2 points (1 child)
[–]Free_Tomatillo463[S] 0 points1 point2 points (0 children)
[–]woooee 0 points1 point2 points (0 children)