I had a mIRC script that i wanted to run in the background, but when debugging with
python, you inevitably need to CTRL+Break out of the program... This however would
break any script(s) running in mIRC.
While there may be few instances where a 'typical' user would want to bypass the safe
halting of mIRC scripts, i do believe my case was a special one. I wanted to take advantage
of mIRC's interfaces to run some scripts... without also having to reinvent the wheel in some
other language. While at the same time, being confident that my scripts or mIRC usage was
not going to cause mIRC to crash due to some looping.
So the reasons for it aside, I found a keyboard handler in python, that allows us to listen for
global hooks to key presses. Then when we detect the "ctrl+break" key... the python script
calls a 'callback' function, and then the user can decide what they want to happen.
We begin with a quick install to keyboard module.
PyPi Reference to keyboard ModuleGithub reference for keyboard moduleor simply install with:
python.exe -m pip install keyboard
Tested on Python 3.7.6
with keyboard module version 0.13.4
Please use at your own risk. I believe this setup to be safe, as i will be using it for myself.
Normal operations are resumed just by closing the cmd.exe window. So in the event that
something does go wrong with your script, you can still easily resume normal operations.
kb_away.py
import keyboard
def key_handler():
return
keyboard.add_hotkey("ctrl+break", key_handler, args=(), suppress=True, timeout=1, trigger_on_release=False)
print("*** Suspending CTRL+Break process ")
keyboard.wait()
kb_away.mrc
alias kb_away {
run cmd.exe /k python.exe $mircdir\kb_away.py
}
alias testkb {
var %i 0
while (%i < 100000) {
inc %i
}
}
Initiate the keyboard procedure with /kb_away
Then you can test that it is operating by pressing /testkb,
This should tie up the scripting for a few seconds, for you to test
whether the CTRL+break is getting through.
Note, to resume normal operations, simply close the python cmd.exe window.
I hope this is helpful to someone.
