Skip to content

Commit

Permalink
Improved hook callback functionality, minor hook class improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
0x2E757 committed Oct 5, 2019
1 parent 42fb469 commit 9905390
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
25 changes: 18 additions & 7 deletions InputInterceptor/Classes/Hook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,27 @@ namespace InputInterceptorNS {

public abstract class Hook<TCallbackStroke> : IDisposable {

public delegate void CallbackAction(ref TCallbackStroke stroke);

public Context Context { get; private set; }
public Device Device { get; private set; }
public Filter FilterMode { get; private set; }
public Predicate Predicate { get; private set; }
public Action<TCallbackStroke> Callback { get; private set; }
public CallbackAction Callback { get; private set; }
public Exception Exception { get; private set; }
public Boolean Active { get; private set; }
public Thread InterceptionThread { get; private set; }

public Hook(Filter filterMode, Predicate predicate, Action<TCallbackStroke> callback) {
public Boolean IsInitialized { get => this.Context != IntPtr.Zero && this.Device != -1; }
public Boolean HasException { get => this.Exception != null; }

public Hook(Filter filterMode, Predicate predicate, CallbackAction callback) {
this.Context = InputInterceptor.CreateContext();
this.Device = -1;
this.FilterMode = filterMode;
this.Predicate = predicate;
this.Callback = callback;
this.Exception = null;
if (this.Context != IntPtr.Zero) {
this.Active = true;
this.InterceptionThread = new Thread(this.Main);
Expand All @@ -42,9 +49,10 @@ private void Main() {
if (InputInterceptor.Receive(this.Context, this.Device = InputInterceptor.WaitWithTimeout(this.Context, 100), ref stroke, 1) > 0) {
if (this.Callback != null && this.Active) {
try {
this.CallbackWrapper(stroke);
this.CallbackWrapper(ref stroke);
} catch (Exception exception) {
Console.WriteLine(exception);
this.Exception = exception;
this.Active = false;
}
} else {
Expand All @@ -59,13 +67,16 @@ private void Main() {
}
}

protected abstract void CallbackWrapper(Stroke stroke);
protected abstract void CallbackWrapper(ref Stroke stroke);

public void Dispose() {
if (this.Active) {
this.Active = false;
this.InterceptionThread.Join();
if (this.Context != IntPtr.Zero) {
if (this.Active) {
this.Active = false;
this.InterceptionThread.Join();
}
InputInterceptor.DestroyContext(this.Context);
this.Context = IntPtr.Zero;
}
}

Expand Down
12 changes: 5 additions & 7 deletions InputInterceptor/KeyboardHook.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
using System;

using Filter = System.UInt16;
using Filter = System.UInt16;

namespace InputInterceptorNS {

public class KeyboardHook : Hook<KeyStroke> {

public KeyboardHook(KeyboardFilter filter = KeyboardFilter.All, Action<KeyStroke> callback = null) :
public KeyboardHook(KeyboardFilter filter = KeyboardFilter.All, CallbackAction callback = null) :
base((Filter)filter, InputInterceptor.IsKeyboard, callback) { }

public KeyboardHook(Action<KeyStroke> callback) :
public KeyboardHook(CallbackAction callback) :
base((Filter)KeyboardFilter.All, InputInterceptor.IsKeyboard, callback) { }

protected override void CallbackWrapper(Stroke stroke) {
this.Callback(stroke.Key);
protected override void CallbackWrapper(ref Stroke stroke) {
this.Callback(ref stroke.Key);
}

}
Expand Down
12 changes: 5 additions & 7 deletions InputInterceptor/MouseHook.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
using System;

using Filter = System.UInt16;
using Filter = System.UInt16;

namespace InputInterceptorNS {

public class MouseHook : Hook<MouseStroke> {

public MouseHook(MouseFilter filter = MouseFilter.All, Action<MouseStroke> callback = null) :
public MouseHook(MouseFilter filter = MouseFilter.All, CallbackAction callback = null) :
base((Filter)filter, InputInterceptor.IsMouse, callback) { }

public MouseHook(Action<MouseStroke> callback) :
public MouseHook(CallbackAction callback) :
base((Filter)MouseFilter.All, InputInterceptor.IsMouse, callback) { }

protected override void CallbackWrapper(Stroke stroke) {
this.Callback(stroke.Mouse);
protected override void CallbackWrapper(ref Stroke stroke) {
this.Callback(ref stroke.Mouse);
}

}
Expand Down

0 comments on commit 9905390

Please sign in to comment.