StrokesPlus.net
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
Rob  
#1 Posted : Tuesday, June 2, 2020 2:11:04 PM(UTC)
Rob

Rank: Administration

Reputation:

Groups: Translators, Members, Administrators
Joined: 1/11/2018(UTC)
Posts: 1,359
United States
Location: Tampa, FL

Thanks: 28 times
Was thanked: 419 time(s) in 356 post(s)
Starting with version 0.3.9.0, you can now subscribe to the low level mouse and keyboard hooks created by StrokesPlus.net via scripts or plug-ins.

Edit: Changed to match 0.3.9.1 alterations.

Note that versions 0.3.9.0 or 0.3.9.1 were not listed as an automatic update due to the risk and need for testing, go to the Downloads page to download it directly.

This is an advanced topic and implementation - you run the risk S+ and system stability if not implemented properly!

User Must Enable Option(s)

Given the nature of exposing these hooks to scripts and plug-ins, the user must opt-in (check) the new option(s) in Options > Advanced for the hooks to be exposed; they are disabled by default.
  • Enable Mouse Hook Event Subscription
  • Enable Keyboard Hook Event Subscription

Synchronous Versus Asynchronous

Events can be subscribed to synchronously or asynchronously, it is recommended to use asynchronous events unless you need to be able to block input.

Synchronous events run in the same thread as the S+ hook and cause the hook to wait (block) until all events are processed. You must use care when using synchronous events as you risk destabilizing S+ and/or Windows if not handled properly.

Asynchronous events run on their own thread and are not blocking, nor can the event be marked for consumption (prevent event from happening in Windows). This is the recommended use as it avoids interfering with the user's keyboard/mouse input and reduces the risk of hanging Windows or S+.

You would only want to use synchronous events if you need to be able to consume the event so it doesn't happen, like a key press or mouse button click which you do not want to be received by Windows or any applications. You must exercise an abundance of caution when using synchronous events as they can create instability within S+ and/or Windows if not carefully implemented.

When using synchronous events, the event args object's .Consume property can be set to true, which tells S+ to instruct Windows that the event was consumed and should not propagate further. The .Consume property has no effect for asynchronous events.

Subscribing to Events

Available events are:
  • MouseHook.OnMouseHookButtonEventAsync
  • MouseHook.OnMouseHookButtonEvent
  • MouseHook.OnMouseHookMoveEventAsync
  • MouseHook.OnMouseHookMoveEvent
  • KeyboardHook.OnKeyboardHookEventAsync
  • KeyboardHook.OnKeyboardHookEvent


When binding via script, note that the event is raised within the engine(s) which bound the event. So if your Max Script Pool Size is set to 3 and you bind the event on load without limiting to a specific engine, the event will be raised 3 times, once in each engine.

It is recommended that for script binding, you only bind in one engine and preferably the last engine in the pool. This reduces the chances that another script is executing in the engine where the event was bound, as the event will not be fired until the currently executing script has finished. For example, if you enabled synchronous events and bound the event in engine #1 while having a long running script executing, S+ will hang and after some time, Windows will evict (remove) the hook and S+ will need to be restarted.

Always wrap your event functions in a try/catch, as exceptions raised within an event can cause S+ to crash.

The example in the spoiler below shows how to bind to the keyboard synchronously and only once in the last available script engine - this script would be placed in the Global Actions > Load/Unload > Load script tab:


Note that the event binding object is stored via sp.StoreObject, this allows other engines to be able to see the object if needed, but it also enables S+ to unbind (disconnect) the event on reload. Keep in mind that events will need to be re-subscribed after reload, which includes clicking Apply or OK in the S+ Settings window.

__spEngineWrapper Object

This was added in release 0.3.9.0 to support determining the engine for the current script, and in case there are other current/future needs for exposing this object. Expand the spoiler to see the properties, note that you should really not alter the object as doing so could cause S+ to become unstable.




Keyboard Hook Events

The event are KeyboardHook.OnKeyboardHookEventAsync and KeyboardHook.OnKeyboardHookEvent.

Looking at the keyboard event binding example above, the keyboardHookEvent object has the following properties:

keyboardHookEvent.Key
- vk as used in other script functions, e.g. vk.VK_Z for the letter 'z'

keyboardHookEvent.KeyState
- KeyState.Down, KeyState.Up, or KeyState.None

keyboardHookEvent.Injected
- true if the event was synthesized - not a physical key press on the attached keyboard

keyboardHookEvent.InjectedByHost
- true if the event was synthesized by StrokesPlus.net, e.g. during a sp.SendKeys call for example

keyboardHookEvent.LowLevelKeyboardHookStruct
- see https://www.pinvoke.net/default.aspx/Structures/KBDLLHOOKSTRUCT.html
- also https://docs.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms644985(v=vs.85)

keyboardHookEvent.wParam (long)
- see https://docs.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms644985(v=vs.85)

keyboardHookEvent.lParam (long)
- see https://docs.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms644985(v=vs.85)

keyboardHookEvent.Consume
- if synchronous events are active, setting this to true in your code will cause the event to be consumed


Note that registered hot keys will not be detected in the hook as those are handled and consumed by Windows. You will see any modifiers leading up to the hot key (e.g. for Control+F10 you would receive the vk.LCONTROL key but not F10).

For unregistered hot keys, the event will be processed prior to S+ handling the event, so if you are using synchronous events and set .Consume to true, S+ will not trigger the hot key as the hook immediately returns if the event function says to consume it.

Simple example which toggles an asynchronous keyboard event binding on and off in the current script engine:



Mouse Hook Events

There are two events for the mouse hook which can be subscribed, mouse click/wheel scroll and mouse move. Only use the mouse move event if you really need to control mouse movement - it generates a LOT of events and can result in noticeably slowing the down the mouse (synchronous) or having unexpected delays (asynchronous) due to the number of executions which need to be made. If you just need positioning, use sp.GetCurrentMousePoint() within your click/wheel event.

Events:
  • MouseHook.OnMouseHookButtonEventAsync (async, button click, wheel scroll)
  • MouseHook.OnMouseHookButtonEvent (sync [blocking], button click, wheel scroll)
  • MouseHook.OnMouseHookMoveEventAsync (async, mouse move)
  • MouseHook.OnMouseHookMoveEvent (sync [blocking], mouse move)


mouseHookEvent args object:

mouseHookEvent.Button
- MouseButtons enum, e.g. MouseButtons.Left

mouseHookEvent.ButtonState
- ButtonState.Down, ButtonState.Up, or ButtonState.None

mouseHookEvent.Location
- Point indicating the location of the mouse on the screen

mouseHookEvent.HorizontalWheel
- true if the wheel scroll was a horizontal directional scroll, false means it was a normal up/down wheel scroll if WhellDelta is also not equal to 0 (zero)

mouseHookEvent.WheelDelta
- Positive or negative number indicating wheel scroll direction, usually in +/- 120 increments

mouseHookEvent.InExclusionZone
- true only when the mouse button or wheel event occurred within a matching user defined exclusion zone. This is always false during mouse move events.

mouseHookEvent.Injected
- true if the event was synthesized - not a physical mouse event from the attached mouse

mouseHookEvent.InjectedByHost
- true if the event was synthesized by StrokesPlus.net, e.g. during a sp.MouseMove/sp.MouseClick call for example

mouseHookEvent.LowLevelKeyboardHookStruct
- see https://pinvoke.net/default.aspx/Structures/MSLLHOOKSTRUCT.html
- also https://docs.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms644986(v=vs.85)

mouseHookEvent.wParam (long)
- see https://docs.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms644986(v=vs.85)

mouseHookEvent.lParam (long)
- see https://docs.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms644986(v=vs.85)

mouseHookEvent.Consume
- if synchronous events are active, setting this to true in your code will cause the event to be consumed


Mouse Button/Wheel script example, synchronous and in the currently executing script engine:



Mouse Move script example, asynchronous and in the currently executing script engine:



Plug-Ins

The examples above should illustrate generally how to utilize these events in a plug-in. You can directly add a reference to StrokesPlus.net.exe and WindowsInput.dll in the StrokesPlus.net installation folder to pull in the classes needed.
Add these declarations in your code:
Code:
using System.Windows.Forms;
using WindowsInput.Native;
using StrokesPlus.net.Hooks;
using StrokesPlus.net.Code;

Edited by user Tuesday, November 24, 2020 1:59:26 PM(UTC)  | Reason: Changed for 0.3.9.1

thanks 2 users thanked Rob for this useful post.
liuxilu on 6/5/2020(UTC), Rob Otter on 11/24/2020(UTC)
liuxilu  
#2 Posted : Wednesday, June 3, 2020 3:05:30 AM(UTC)
liuxilu

Rank: Advanced Member

Reputation:

Groups: Translators, Moderators, Approved
Joined: 4/6/2020(UTC)
Posts: 79
China

Thanks: 1 times
Was thanked: 21 time(s) in 21 post(s)
I just realized to have individual plugin/script event and sync setting, then it can pass reference to plugins, vlaue to scripts - without unstable behavior with scripts and effective passing for plugins.
By adding CallNextHook functionality right after the sync event call, it will also allow plugins to change the input

Edited by user Wednesday, June 3, 2020 3:11:01 AM(UTC)  | Reason: Not specified

Rob Otter  
#3 Posted : Tuesday, November 24, 2020 1:27:12 PM(UTC)
Rob Otter

Rank: Advanced Member

Reputation:

Groups: Approved
Joined: 10/26/2020(UTC)
Posts: 50
Germany
Location: Darmstadt

Thanks: 15 times
Was thanked: 2 time(s) in 2 post(s)
When I use the async keyboard event binding script given above in the global Load script section, I get the following error when saving (no changes have been made to your sample code):

Failed to process load automation. Check your Load Automation for errors:
Error: The object has no suitable property or field named 'EnableSyncHookEvents'
at LoadScripts:40:39 ->
KeyboardHook.EnableSyncHookEvents = false; //async

Keyboard Hook is enabled in Options.
Rob  
#4 Posted : Tuesday, November 24, 2020 2:00:04 PM(UTC)
Rob

Rank: Administration

Reputation:

Groups: Translators, Members, Administrators
Joined: 1/11/2018(UTC)
Posts: 1,359
United States
Location: Tampa, FL

Thanks: 28 times
Was thanked: 419 time(s) in 356 post(s)
Oops, remove that line - I forgot to take that out when I updates the scripts snippets for 0.3.9.1
kuqijun  
#5 Posted : Tuesday, July 12, 2022 9:27:47 AM(UTC)
kuqijun

Rank: Newbie

Reputation:

Groups: Approved
Joined: 7/12/2022(UTC)
Posts: 4
China

Thanks: 1 times
鼠标移动脚本示例,异步和当前正在执行的脚本引擎中:

不好意思,这里的演示不太懂,可以写的小白一点吗。。就是我改怎么去用鼠标移动到所在位置,去激活一个事件呢。就是把鼠标的移动当作手势键。。

我看你这个贴子好像是这个意思,但是不知道你写的脚本改如何使用。
Rob  
#6 Posted : Wednesday, July 13, 2022 2:25:19 PM(UTC)
Rob

Rank: Administration

Reputation:

Groups: Translators, Members, Administrators
Joined: 1/11/2018(UTC)
Posts: 1,359
United States
Location: Tampa, FL

Thanks: 28 times
Was thanked: 419 time(s) in 356 post(s)
Originally Posted by: kuqijun Go to Quoted Post
Mouse move script example, asynchronously and in the currently executing script engine:

I'm sorry, I don't understand the demo here. Can you write a little bit more? . It is how I change how to use the mouse to move to the location to activate an event. Is to use the mouse movement as a gesture key. .

I see that your post seems to mean this, but I don't know how to use the script you wrote.


These scripts are simply to hook (observe) all mouse messages that S+ received from Windows so you can act on them, if it is something which cannot be done via the normal functionality built into S+.

If there's something specific you are trying to accomplish, let us know and we will try to help.

Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.