Firstly, try not to revive old threads like this. They're great for reference but if you have a particular question about it, it's better to start a new thread.
Now, working with global hotkeys is not a very simple thing to do. Countdown Live includes a class which makes this a lot easier to do, but it's still pretty advanced stuff. The following steps should be followed up precisely, otherwise you might crash Messenger.
In the latest version of Countdown Live, you'll find a file called GlobalHotkey.js which contains all logic for working with system-wide hotkeys. To use this class, you'll have to do a few things:
- Copy "GlobalHotkey.js" from Countdown Live to your script folder.
You'll have to change one thing though. Find this line:
js code:
Trace(bPaused ? "> Global hotkeys paused" : "> Global hotkeys re-enabled");
and remove it. This was just some debug code which uses my custom Trace() function. There's no need to keep this line, but if you do keep it you'll need to have a Trace() function as well. Therefore, it's better to just delete it.
- In order to capture hotkey presses, the class needs to create a subclass window to listen to window notifications. Therefore, you need to copy "WndSubclass.xml" from the Interfaces folder as well. This window won't be visible in any way but is required to capture these notifications.
- Pick a key combination to use. In this example, I'll use Ctrl+Alt+R as an example.
- Look up the virtual-key codes for your chosen combination. You can find all key codes with their hexadecimal codes on MSDN. For the sample combination, you need the key code for "A" which is 0x41.
- In your OnEvent_Initialize handler, you need to add some code to correctly start the class.
- In the subclass configuration, make sure the InterfacePath and WindowId properties correctly point to the subclass window.
- The registry functions are used to clean up hot keys after the script has suddenly stopped working. I put in two simply functions which read and write to a "SubclassHandle" key value inside the script's registry key. If you already use another set of registry functions, you can replace the code in these handlers with your custom registry calls. Just make sure that they work!
- The interesting part is where you create the hotkey. Here, you use the virtual-key code for the main key and some constants for the key modifiers (MOD_CTRL, MOD_ALT, MOD_SHIFT and/or MOD_WIN). You also need to specify a function which will be called when the hotkey is pressed. For this example, I used an anonymous function which writes something to the debug window and then calls your reFloat() function. (You'll probably want to check whether the current user is logged in before you call reFloat() though.)
js code:
function OnEvent_Initialize(MessengerStart) {
// Subclass configuration
GlobalHotkey.Subclass.InterfacePath = "WndSubclass.xml";
GlobalHotkey.Subclass.WindowId = "WndSubclass";
GlobalHotkey.Subclass.Registry.GetValue = function() {
new ActiveXObject("WScript.Shell").RegRead( MsgPlus.ScriptRegPath + "SubclassHandle");
};
GlobalHotkey.Subclass.Registry.SetValue = function(handle) {
new ActiveXObject("WScript.Shell").RegWrite( MsgPlus.ScriptRegPath + "SubclassHandle", handle, "REG_DWORD");
};
// Hotkey registration
var HotkeyID = "MyPlusScript::reFloat"; // Unique identifier
var KeyCode = 0x41; // A key
var Modifiers = MOD_CONTROL | MOD_ALT; // Ctrl + Alt
var Callback = function() {
Debug.Trace("Hotkey pressed!");
reFloat();
};
GlobalHotkey.Add(HotkeyID, KeyCode, Modifiers, Callback);
// Configuration is done, now get this thing started
GlobalHotkey.Init();
}
- Add this cleanup code to your OnEvent_Uninitialize handler:
js code:
function OnEvent_Uninitialize(MessengerExit) {
GlobalHotkey.DeleteAll();
GlobalHotkey.Destroy();
}
- Finally, you need to add the hotkey notification handler to the notification event of the subclass window.
js code:
function OnWndSubclassEvent_MessageNotification(PlusWnd, Message, wParam, lParam) {
if(GlobalHotkey.HandleNotification(arguments)) return -1;
}
Yes, that's a pretty long list but I believe you should be able to pull this off if you have a bit of scripting experience.