Equivalent to SuspendKeys on Python?

Post a reply


In an effort to prevent automatic submissions, we require that you complete the following challenge.
Smilies
:D :) ;) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :!: :?: :idea: :| :mrgreen: :geek: :ugeek: :arrow: :angel: :clap: :crazy: :eh: :lolno: :problem: :shh: :shifty: :sick: :silent: :think: :thumbup: :thumbdown: :salute: :wave: :wtf: :yawn: :facepalm: :bravo: :dance: :beard: :morebeard: :xmas: :HeHe: :trollface: :cookie: :rainbow: :monkeysee: :monkeysay: :happybday: :headwall: :offtopic: :superhappy: :terms: :beer:
View more smilies

BBCode is ON
[img] is OFF
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Equivalent to SuspendKeys on Python?

Equivalent to SuspendKeys on Python?

Post by aaurteo » 29 Mar 2024, 06:31

Hello!

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.
	Suspend
	if (ifenabled = 0) {
		ifenabled := 1
		TrayTip 
		TrayTip, % "The app", % "Desabled", , 16
	} else {
		ifenabled := 0
		TrayTip
		TrayTip, % "The app", % "Enabled", , 16
	}
Return

^+!F1:: ; control + shift + alt + F1
	Suspend, Permit
Return
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!

Thanks!

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__":
    main()


Top