/* * ---------------------------------------------------------------------------- * "THE BEER-WARE LICENSE" (Revision 42): * wrote this file. As long as you retain this notice you * can do whatever you want with my code. If we meet some day, and you think * this code is worth it, you can buy me a beer in return - Matt Labrum (-dt-) * ---------------------------------------------------------------------------- */ /* $LastChangedBy: dt $ $LastChangedDate: 2007-11-02 20:49:38 -0700 (Fri, 02 Nov 2007) $ $LastChangedRevision: 321 $ $HeadURL: http://version.thedt.net/scripts/plusscripts/musicNowplaying/hotkeys.js $ */ var _hotkeys = {}; var _hotkeywnd = false; var WM_HOTKEY = 0x312; var WM_USER = 0x400; function OnWndMessageHotKeyEvent_MessageNotification(wnd, message, wParam, lParam){ switch(message){ case WM_HOTKEY : Debug.Trace('hotkey'); triggerEvent('hotkey_' + wParam); return -1; break; } } function registerGlobalHotkey(key, func){ if(!_hotkeywnd){ _hotkeywnd = MsgPlus.CreateWnd("interface.xml", "WndMessageHotkey", 2); _hotkeywnd.RegisterMessageNotification(WM_HOTKEY, true); } var id = getRegisteredHotkeysLength() Debug.Trace("xxxx: " + key); if(Interop.Call("User32", "RegisterHotKey", _hotkeywnd.Handle, id, hibyte(key)&~8, lobyte(key))){ //Debug.Trace(hibyte2modkey(hibyte(key))+' + '+lobyte2key(lobyte(key))); addEventListener("hotkey_" + id, id, func); }else{ Debug.Trace('Failed: ' + GetKeyName(key)); } _hotkeys[key] = id; } function hotkeyGetKeyFromId(id){ for(x in _hotkeys){ if(_hotkeys[x] == id)return x; } } function getRegisteredHotkeysLength(){ var num=0; for(x in _hotkeys){num++;} return num; } function unRegisterGlobalHotkey(key){ if(typeof(_hotkeys[key]) != "undefined"){ Interop.Call("User32", "UnregisterHotKey", _hotkeywnd.Handle, _hotkeys[key]); removeEventListener("hotkey_" + _hotkeys[key], "_main_"); delete _hotkeys[key]; } if(getRegisteredHotkeysLength == 0){ _hotkeywnd.Close(0); _hotkeywnd = false; } } function unRegisterAllGlobalHotkeys(){ for(x in _hotkeys){ unRegisterGlobalHotkey(x); } } /* some misc functions by matty */ var HK_GETKEY = (WM_USER+2); var HK_SETKEY = (WM_USER+1); /* Name: GetKeyName Purpose: Creates a readable string based on the numerical value of the hotkey Parameters: uCode - The numerical value of the hotkey Return: Readable string of the hotkey */ function GetKeyName(uCode){ var ScanCode = Interop.Call('user32', 'MapVirtualKeyW', lobyte(uCode), 0); if(hibyte(uCode) & 8) ScanCode |= 0x100; var Text = Interop.Allocate(512); Interop.Call('user32', 'GetKeyNameTextW', ScanCode << 16, Text, 255); var Prefix = (hibyte(uCode) & 2 ? 'Ctrl + ' : '') + (hibyte(uCode) & 1 ? 'Shift + ' : '') + (hibyte(uCode) & 4 ? 'Alt + ' : ''); return Prefix + Text.ReadString(0); } function lobyte(w){ return w & 0xff; } function hibyte(w){ return w >> 8; } function makeword(a,b){ return a | (b << 8); }