/*
* -----
* Screenshot Sender - __hotkey.js
* -----
* Hotkey functions for Screenshot Sender 5
* -----
*/
var Hotkeys = function () {
this.WM_HOTKEY = 0x312;
this.HK_SETKEY = 0x401;
this.HK_GETKEY = 0x402;
this.MOD_WIN = 0x8;
this.MOD_ALT = 0x1;
this.MOD_CONTROL = 0x2;
this.MOD_SHIFT = 0x4;
//Subclass window information
GlobalHotkey.Subclass.Registry.GetValue = function () {
Registry_GetKeyValue(HKCU, RegistryInit(), 'SubclassHandle');
};
GlobalHotkey.Subclass.Registry.SetValue = function (hWndSubclass) {
Registry_SetKeyValue(HKCU, RegistryInit(), 'SubclassHandle', hWndSubclass, REG_DWORD);
};
//Actions
GlobalHotkey.Actions.Add("BeforeRegister", function () {
_debug.trace("> Start registering hotkeys");
});
GlobalHotkey.Actions.Add("RegisterHotkey", function (oHotkey, bSuccess) {
_debug.trace(" '"+oHotkey.Id+"' -> '"+oHotkey.KeyName+"' ..."+ (bSuccess?"Success!":"Error!") );
});
GlobalHotkey.Actions.Add("AfterRegister", function (bSuccess, aFailed) {
_debug.trace("> Global hotkeys registration "+ (bSuccess?"succesfully completed":"failed!") );
if (!bSuccess) _debug.trace(" "+ (aFailed ? aFailed.length : 0) +" hotkey(s) failed to register");
});
}
Hotkeys.prototype = {
"Initialize" : function () {
// Don't do anything when already initialized
if (GlobalHotkey.IsInit() ) return;
// If the file exists in the current translation, load it
var Name = 'Hotkey_Subclass';
var FilePath = InterfacePath + (typeof objPreferences['cLanguage'] === 'undefined' ? MsgPlus.UILangCode : objPreferences['cLanguage']) + '\\' + Name + '.xml';
if (FileExists(FilePath) == false) {
// Otherwise, load the default translation(en)
FilePath = InterfacePath + 'en\\' + Name + '.xml';
}
// Set the hotkey subclass details
GlobalHotkey.Subclass.InterfacePath = '\\' + FilePath;
GlobalHotkey.Subclass.WindowId = Name;
// Initialize the class
GlobalHotkey.Init();
},
"LoadHotkeys" : function () {
// Delete all registered hotkeys
GlobalHotkey.DeleteAll();
// Collect the hotkeys from the registry
var objSubkeys = {};
Registry_EnumSubkeys(HKCU, RegistryInit() +'\\Hotkeys\\', objSubkeys);
for (var objSubkey in objSubkeys) {
if (Registry_GetKeyValue(HKCU, RegistryInit() +'\\Hotkeys\\'+objSubkey, 'bEnabled') === 1) {
var lKeyCode = Registry_GetKeyValue(HKCU, RegistryInit() +'\\Hotkeys\\'+objSubkey, 'Hotkey');
var lAction = Registry_GetKeyValue(HKCU, RegistryInit() +'\\Hotkeys\\'+objSubkey, 'Action');
var fCallback = this.GetHotkeyCallback(lAction);
GlobalHotkey.Add(Messenger.MyUserId+':'+lKeyCode, lKeyCode, 0, fCallback);
}
}
// Reload the hotkeys
GlobalHotkey.Reload();
},
"GetHotkeyCallback" : function (lAction) {
return this.BindFunction (function (oHotkey) {
_debug.trace("Hotkey "+oHotkey.KeyName+" pressed - lAction = "+lAction);
this.ProcessHotkeyAction(lAction);
}, this);
},
"PauseHotkeyHandling" : function (bPaused) {
GlobalHotkey.Break(bPaused);
},
"UnregisterHotkeys" : function (hWnd) {
GlobalHotkey.DeleteAll();
GlobalHotkey.Destroy();
},
"GetKeyName" : function (uCode) {
return GlobalHotkey.GetKeyName(uCode);
},
"ProcessHotkeyMessage" : function (pPlusWnd, nMessage, wParam, lParam) {
return GlobalHotkey.HandleNotification(arguments);
},
"ProcessHotkeyAction" : function (lAction, bPrntScrn) {
if (typeof bPrntScrn === 'undefined') bPrntScrn = false;
var oGdip = new Gdip();
oGdip.Initialize();
var bSave; // set to true if the hotkey is for saving the image false if its for sending
var bDelay = false; // used to check if the hotkey causes a delay such as select area
switch (lAction) {
case 0: oGdip.SaveImage(oGdip.CaptureWindow_Active(), SessionImages.CreateTempImage() ); bSave = false; break;
case 1: oGdip.SaveImage(oGdip.CaptureWindow_Active(), SessionImages.CreateTempImage() ); bSave = true; break;
case 2: oGdip.SaveImage(oGdip.CaptureWindow_Fullscreen(), SessionImages.CreateTempImage() ); bSave = false; break;
case 3: oGdip.SaveImage(oGdip.CaptureWindow_Fullscreen(), SessionImages.CreateTempImage() ); bSave = true; break;
case 4: oGdip.SaveImage(oGdip.CaptureWindow_Clipboard(), SessionImages.CreateTempImage() ); bSave = false; break;
case 5: oGdip.SaveImage(oGdip.CaptureWindow_Clipboard(), SessionImages.CreateTempImage() ); bSave = true; break;
case 6: SelectedArea(0, 'ssselect', 0, false); bSave = false; bDelay = true; break;
case 7: SelectedArea(0, 'sssselect', 0, true); bSave = true; bDelay = true; break;
case 8: PointClickCapture(0, 'sspcc', 0, false); bSave = false; bDelay = true; break;
case 9: PointClickCapture(0, 'ssspcc', 0, true); bSave = true; bDelay = true; break;
default: oGdip.Uninitialize(); return false;
}
if (bDelay == false && objPreferences['cPreviews'] == true) Preview(0, oGdip.ImageLocation, bSave);
if (bPrntScrn == true) oGdip.Clipboard_SetData(oGdip.hBitmap);
oGdip.Uninitialize();
},
"LoadConfiguredHotkeys" : function (pPlusWnd, sControlId) {
var objSubkeys = {}, nIndex = 0;
Registry_EnumSubkeys(HKCU, RegistryInit() +'\\Hotkeys\\', objSubkeys);
for (var objSubkey in objSubkeys) {
var bEnabled = Registry_GetKeyValue(HKCU, RegistryInit() +'\\Hotkeys\\'+objSubkey, 'bEnabled');
nIndex = pPlusWnd.LstView_AddItem(sControlId, _lang.text['HotkeyActions_'+Registry_GetKeyValue(HKCU, RegistryInit() +'\\Hotkeys\\'+objSubkey, 'Action')]);
pPlusWnd.LstView_SetItemText(sControlId, nIndex, 1, this.GetKeyName(Registry_GetKeyValue(HKCU, RegistryInit() +'\\Hotkeys\\'+objSubkey, 'Hotkey') ) );
pPlusWnd.LstView_SetItemText(sControlId, nIndex, 2, objSubkey);
pPlusWnd.LstView_SetItemText(sControlId, nIndex, 3, bEnabled);
pPlusWnd.LstView_SetItemIcon(sControlId, nIndex, bEnabled, true);
}
},
"AddAtom" : function (_string) { return Interop.Call('kernel32', 'GlobalAddAtomW', _string); },
"GetAtom" : function (_string) { return Interop.Call('kernel32', 'GlobalFindAtomW', _string); },
"GetAtomName" : function (_atom) { var _sBuffer = Interop.Allocate(512); Interop.Call('kernel32', 'GlobalGetAtomNameW', _atom, _sBuffer, 512); return _sBuffer.ReadString(0); },
"DeleteAtom" : function (_atom) { Interop.Call('kernel32', 'GlobalDeleteAtom', _atom.toString(16) ); },
"lobyte" : function (w) { return w & 0xff; },
"hibyte" : function (w) { return w >> 8; },
"makeword" : function (a,b) { return a | (b << 8); },
//Binds a function to an object - based on Function.bind from Prototype(www.prototypejs.org)
"BindFunction" : function () {
var args = toArray(arguments);
var func = args.shift();
var obj = args.shift();
return function () {
return func.apply(obj, args.concat(toArray(arguments) ) );
}
function toArray(iterable) {
if (!iterable) return[];
if (iterable.toArray) return iterable.toArray();
var length = iterable.length, results = new Array(length);
while (length--) results[length] = iterable[length];
return results;
}
}
}