Originally Posted by: Rob Thank you, Rob.
And after I research the code in the threat you gave, I modified a good script which you can use for sample to give more keys more usage:
//===== 多次按左Ctrl执行不同命令,开始
// 参考:https://forum.strokesplus.net/posts/t8266findunread-Double-CTRL-press-as-hotkey---a-good-approach,
//参考:
https://forum.strokesplu...oard-Event-Subscriptions// This subscribes for an asynchronous keyboard hook event
// Dont forget to enable Keyboard Hook Event Subscription in Options!
var keyboardHookEventLctrl = sp.GetStoredObject("keyboardEvent");
var keyPressTimeoutLctrl = 1000; // 1 second
sp.StoreNumber("LctrlKeyPressCounter", 0);
if (!keyboardHookEventLctrl.GetType().FullName.includes('EventConnection')) {
var keyboardEventLctrl = KeyboardHook.OnKeyboardHookEventAsync.connect(
function (sender, keyboardHookEvent) {
try {
if (keyboardHookEvent.Key == vk.LCONTROL && keyboardHookEvent.KeyState == KeyState.Up) {
sp.StoreNumber("LctrlKeyPressCounter", sp.GetStoredNumber("LctrlKeyPressCounter") + 1);
if (sp.GetStoredNumber("LctrlKeyPressCounter") == 1) {
// Start Timer
sp.CreateTimer('LctrlKeyTimeout', keyPressTimeoutLctrl, -1, String.raw`
// React on number of key presses:
switch (sp.GetStoredNumber("LctrlKeyPressCounter")) {
case 1:
// Don´t do anything to keep original function and not interfere with several
// hotkey combinations (ctrl-c, ctrl-s, ...)
break;
case 2:
// Don´t do anything to keep original function and not interfere with several
break;
case 3:
// Do some amazing stuff, 连续按3次左ctrl,执行以下自定义的命令。由于两次ctrl是Listary的热
//键,Listary会获得焦点,所以多次左ctrl执行的命令应为不需要特定焦点的全局命令或宏。
// eval(sp.GetMacroScript("Functions", "MouseRestrict1D"));
sp.SendModifiedVKeys([vk.LWIN], [vk.VK_E]); //连续按3次左ctrl 为打开我的电脑
break;
case 4:
// Do some amazing stuff,暂时空置
sp.SendVKey(vk.VOLUME_MUTE); //连续按4次左ctrl 为切换静音
break;
default:
// sp.MessageBox("# pressed L-Ctrl: " + sp.GetStoredNumber("LctrlKeyPressCounter"), "Message from S+");
var currpt = sp.GetCurrentMousePoint();var info = new DisplayTextInfo();
info.Title = "一共" + sp.GetStoredNumber("LctrlKeyPressCounter") + "次";
info.Message = "已连续按左Ctrl键" ;
info.Location = (currpt.X-100)+','+(currpt.Y-150);
info.Duration = 2000;info.TitleAlignment = "Center";info.Opacity = 0.7;info.MessageAlignment = "Left";info.TitleFont = new Font("Segoe UI", 33, host.flags(FontStyle.Bold));info.MessageFont = new Font("Segoe UI Semibold", 17);info.BackColor = "black";info.ForeColor = "white";info.Padding = 5;
sp.DisplayText(info);
break;
}
sp.StoreNumber("LctrlKeyPressCounter", 0);
sp.DeleteTimer('LctrlKeyTimeout');
`);
}
}
}
catch(err) {
sp.MessageBox("An error occured while processing L-CTRL hotkey:\n\n" + err.message, "Message from S+");
}
}
);
sp.StoreObject("keyboardEvent", keyboardEventLctrl);
}
//=====多次按左Ctrl执行不同命令,结束
//===== 多次按右Ctrl执行不同命令,开始
// 参考:https://forum.strokesplus.net/posts/t8266findunread-Double-CTRL-press-as-hotkey---a-good-approach,
//参考:
https://forum.strokesplu...oard-Event-Subscriptions// This subscribes for an asynchronous keyboard hook event
// Dont forget to enable Keyboard Hook Event Subscription in Options!
var keyboardHookEventRctrl = sp.GetStoredObject("keyboardEventRctrl");
var keyPressTimeoutRctrl = 1000; // 1 second
sp.StoreNumber("RctrlKeyPressCounter", 0);
if (!keyboardHookEventRctrl.GetType().FullName.includes('EventConnection')) {
var keyboardEventRctrl = KeyboardHook.OnKeyboardHookEventAsync.connect(
function (sender, keyboardHookEventRctrl) {
try {
if (keyboardHookEventRctrl.Key == vk.RCONTROL && keyboardHookEventRctrl.KeyState == KeyState.Up) {
sp.StoreNumber("RctrlKeyPressCounter", sp.GetStoredNumber("RctrlKeyPressCounter") + 1);
if (sp.GetStoredNumber("RctrlKeyPressCounter") == 1) {
// Start Timer
sp.CreateTimer('RctrlKeyTimeout', keyPressTimeoutRctrl, -1, String.raw`
// React on number of key presses:
switch (sp.GetStoredNumber("RctrlKeyPressCounter")) {
case 1:
// Don´t do anything to keep original function and not interfere with several
// hotkey combinations (ctrl-c, ctrl-s, ...)
break;
case 2:
// Don´t do anything to keep original function and not interfere with several
break;
case 3:
// Do some amazing stuff, 连续按3次右ctrl,执行以下自定义的命令。由于两次ctrl是Listary的热
//键,Listary会获得焦点,所以多次右ctrl执行的命令应为不需要特定焦点的全局命令或宏。
// eval(sp.GetMacroScript("Functions", "MouseRestrict1D"));
sp.SendModifiedVKeys([vk.LWIN], [vk.VK_E]); //连续按3次右ctrl 为打开我的电脑
break;
case 4:
// Do some amazing stuff,暂时空置
sp.SendVKey(vk.VOLUME_MUTE); //连续按4次右ctrl 为切换静音
break;
default:
// sp.MessageBox("# pressed R-Ctrl: " + sp.GetStoredNumber("RctrlKeyPressCounter"), "Message from S+");
var currpt = sp.GetCurrentMousePoint();var info = new DisplayTextInfo();
info.Title = "一共" + sp.GetStoredNumber("RctrlKeyPressCounter") + "次";
info.Message = "已连续按右Ctrl键" ;
info.Location = (currpt.X-100)+','+(currpt.Y-150);
info.Duration = 2000;info.TitleAlignment = "Center";info.Opacity = 0.7;info.MessageAlignment = "Left";info.TitleFont = new Font("Segoe UI", 33, host.flags(FontStyle.Bold));info.MessageFont = new Font("Segoe UI Semibold", 17);info.BackColor = "black";info.ForeColor = "white";info.Padding = 5;
sp.DisplayText(info);
break;
}
sp.StoreNumber("RctrlKeyPressCounter", 0);
sp.DeleteTimer('RctrlKeyTimeout');
`);
}
}
}
catch(err) {
sp.MessageBox("An error occured while processing R-CTRL hotkey:\n\n" + err.message, "Message from S+");
}
}
);
sp.StoreObject("keyboardEventRctrl", keyboardEventRctrl);
}
//=====多次按右Ctrl执行不同命令,结束
//=====可以开发可多次按下执行不同命令的按键有:Alt, Tab键上方的`键,F1~F12,只需要用上面的代码模板搜索替换Rctrl与vk.RCONTROL即可。
// You can give Alt, `,F1~F12 difinations by using the above sample code with search and repleace "Rctrl" and vk.RCONTROL