Equivalent to SuspendKeys on Python?

Discuss other programming languages besides AutoHotkey
Posts: 9
Joined: 28 Dec 2015, 01:42

Equivalent to SuspendKeys on Python?

Post by aaurteo » 29 Mar 2024, 06:31


On AutoHotKey I do something like this using SuspendKeys to turn on or off the script of a list of shortcuts.

Code: Select all

ifenabled := 0

SuspendKeys: ; by default, this app is suspended. And when it's called by ^+!F1, it will be activated.
	if (ifenabled = 0) {
		ifenabled := 1
		TrayTip, % "The app", % "Desabled", , 16
	} else {
		ifenabled := 0
		TrayTip, % "The app", % "Enabled", , 16

^+!F1:: ; control + shift + alt + F1
	Suspend, Permit
Is there any recommended way to do the same thing on Python?

ChatGPT showed me this code. Is it the best way? I like AutoHotKeys SuspendKeys because it truly turns off the shortcuts!


Code: Select all

import keyboard

# Global variable to track the active state of the script
active = True

def toggle_activation():
    global active
    active = not active
    print("Script is now", "active" if active else "inactive")

def take_screenshot():
    if active:
        print("Taking screenshot...")  # Replace this with your screenshot logic

def perform_task():
    if active:
        print("Performing task...")  # Replace this with your task logic

def main():
    # Listen for Ctrl+J key combination to toggle activation
    keyboard.add_hotkey('ctrl+j', toggle_activation)

    # Listen for other key combinations to perform actions
    keyboard.add_hotkey('x', take_screenshot)
    keyboard.add_hotkey('z', perform_task)

    print("Script is now active")
    keyboard.wait()  # Wait for keyboard events indefinitely

if __name__ == "__main__":

Return to “Other Programming Languages”