Shoutbox

HotKeys ? - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: HotKeys ? (/showthread.php?tid=86931)

HotKeys ? by Xat on 10-28-2008 at 05:51 PM

Hi !

I want to use HotKeys in my script...

I want to my script do the "ALT+C" HotKey.

How can I do (don't redirect me here please).

Thanks


RE: HotKeys ? by matty on 10-28-2008 at 06:26 PM

Matti is working on a hotkey class that will explain very easily and allow you to use them easily as well. Not sure when he is going to post it though.


RE: HotKeys ? by Matti on 10-28-2008 at 06:45 PM

Well yeah, I'm indeed working on a full-blown class which allows easy creation and management of system-wide hotkeys, that means: hotkeys which will work from anywhere, not exclusively in one window.

I'm planning to release this along with the next version of Countdown Live, although if I see that the script's development is taking very long, I might consider releasing this class a bit earlier. The class can still use some more polishing, but most of the time I first have to make a serious application built around it to test if the class functions well enough. I don't want to release the class and then come to the conclusion that I better restructure it, since then all developers who downloaded the first version have to redo their scripts also. ;)

No worries, it'll be here when it's done... :P


RE: RE: HotKeys ? by V@no on 11-27-2010 at 04:59 AM

quote:
Originally posted by Matti
Well yeah, I'm indeed working on a full-blown class which allows easy creation and management of system-wide hotkeys, that means: hotkeys which will work from anywhere, not exclusively in one window.

I'm planning to release this along with the next version of Countdown Live
Would you please give an working example how to use your class?

As I understand there must be a window or control where message will be sent when pressed hotkey, am I correct? if so, what would I need to do if I want just execute a JS function when hotkey pressed without any extra window?
Basically I have this script, and I need execute reFloat() function when pressed a hotkey:

code:
function reFloat()
{
  var c = Messenger.MyContacts;
  for (var i = new Enumerator(c); !i.atEnd(); i.moveNext())
  {
    var n = i.item();
    if (n.IsFloating)
    {
      n.IsFloating = false;
      n.IsFloating = true;
    }
  }
}
function OnEvent_Initialize(MessengerStart)
{
  reFloat();
}

function OnEvent_Uninitialize(MessengerExit)
{
}


Thank you.


P.S.
I'm trying create a work around for this issue: Desktop contacts won't stay ontop
RE: HotKeys ? by Matti on 11-27-2010 at 10:45 AM

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. :P

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:

  1. 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.
  2. 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.
  3. Pick a key combination to use. In this example, I'll use Ctrl+Alt+R as an example.
  4. 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.
  5. 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();
    }
  6. Add this cleanup code to your OnEvent_Uninitialize handler:
    js code:
    function OnEvent_Uninitialize(MessengerExit) {
        GlobalHotkey.DeleteAll();
        GlobalHotkey.Destroy();
    }
  7. 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. :)
RE: HotKeys ? by V@no on 11-27-2010 at 05:47 PM

Thank you very much!
Your example works perfectly. Just three things had to be corrected:
Interfaces.xml should be WndSubclass.xml
MOD_CTRL should be MOD_CONTROL
and second registry function is supposed to be SetValue not GetValue :)

Thank you again.


RE: HotKeys ? by Matti on 11-28-2010 at 11:15 AM

Well, yeah, that was a test. You passed it, congratulations. :P