Skip to content

Commit b5b1415

Browse files
Add the ability to disable key typed events
1 parent 4a92440 commit b5b1415

File tree

5 files changed

+119
-0
lines changed

5 files changed

+119
-0
lines changed

SharpHook/Native/UioHook.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,52 @@ public static partial class UioHook
331331
public static extern void SetPostTextDelayX11(ulong delayNanoseconds);
332332
#endif
333333

334+
/// <summary>
335+
/// Checks whether events of type <see cref="EventType.KeyTyped" /> are enabled.
336+
/// </summary>
337+
/// <returns>
338+
/// <see langword="true" /> if events of type <see cref="EventType.KeyTyped" /> are enabled. Otherwise,
339+
/// <see langword="false" />.
340+
/// </returns>
341+
/// <remarks>
342+
/// If the application doesn't use events of type <see cref="EventType.KeyTyped" />, then they should be disabled
343+
/// so that there is no performance penalty and no subtle system-wide side effects.
344+
/// </remarks>
345+
#if NET7_0_OR_GREATER
346+
[LibraryImport(LibUioHook, EntryPoint = "hook_is_key_typed_enabled")]
347+
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
348+
[return: MarshalAs(UnmanagedType.I1)]
349+
public static partial bool IsKeyTypedEnabled();
350+
#else
351+
[DllImport(LibUioHook, EntryPoint = "hook_is_key_typed_enabled", CallingConvention = CallingConvention.Cdecl)]
352+
[return: MarshalAs(UnmanagedType.I1)]
353+
public static extern bool IsKeyTypedEnabled();
354+
#endif
355+
356+
/// <summary>
357+
/// Sets the value which indicates whether events of type <see cref="EventType.KeyTyped" /> are enabled. The default
358+
/// value is <see langword="true" />.
359+
/// </summary>
360+
/// <param name="enabled">
361+
/// <see langword="true" /> if events of type <see cref="EventType.KeyTyped" /> should be enabled. Otherwise,
362+
/// <see langword="false" />.
363+
/// </param>
364+
/// <remarks>
365+
/// If the application doesn't use events of type <see cref="EventType.KeyTyped" />, then they should be disabled
366+
/// so that there is no performance penalty and no subtle system-wide side effects.
367+
/// </remarks>
368+
#if NET7_0_OR_GREATER
369+
[LibraryImport(LibUioHook, EntryPoint = "hook_set_key_typed_enabled")]
370+
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
371+
public static partial void SetKeyTypedEnabled([MarshalAs(UnmanagedType.I1)] bool enabled);
372+
#else
373+
[DllImport(
374+
LibUioHook,
375+
EntryPoint = "hook_set_key_typed_enabled",
376+
CallingConvention = CallingConvention.Cdecl)]
377+
public static extern void SetKeyTypedEnabled([MarshalAs(UnmanagedType.I1)] bool enabled);
378+
#endif
379+
334380
/// <summary>
335381
/// Checks whether access to macOS Accessibility API is enabled for the process, optionally prompting the user
336382
/// if it is disabled.

SharpHook/Providers/IGlobalHookProvider.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ namespace SharpHook.Providers;
55
/// </summary>
66
public interface IGlobalHookProvider
77
{
8+
/// <summary>
9+
/// Gets or sets the value which indicates whether events of type <see cref="EventType.KeyTyped" /> are enabled. The
10+
/// default value is <see langword="true" />.
11+
/// </summary>
12+
/// <value>
13+
/// <see langword="true" /> if events of type <see cref="EventType.KeyTyped" /> are enabled. Otherwise,
14+
/// <see langword="false" />.
15+
/// </value>
16+
bool KeyTypedEnabled { get; set; }
17+
818
/// <summary>
919
/// Sets the hook callback function.
1020
/// </summary>

SharpHook/Providers/UioHookProvider.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ private UioHookProvider()
2121
/// </summary>
2222
public static UioHookProvider Instance { get; } = new();
2323

24+
/// <summary>
25+
/// Gets or sets the value which indicates whether events of type <see cref="EventType.KeyTyped" /> are enabled. The
26+
/// default value is <see langword="true" />.
27+
/// </summary>
28+
/// <value>
29+
/// <see langword="true" /> if events of type <see cref="EventType.KeyTyped" /> are enabled. Otherwise,
30+
/// <see langword="false" />.
31+
/// </value>
32+
public bool KeyTypedEnabled
33+
{
34+
get => UioHook.IsKeyTypedEnabled();
35+
set => UioHook.SetKeyTypedEnabled(value);
36+
}
37+
2438
/// <summary>
2539
/// Gets or sets the delay (in nanoseconds) between posting individual characters when posting text on Linux.
2640
/// </summary>

SharpHook/SharpHook.xml

Lines changed: 47 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SharpHook/Testing/TestProvider.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,8 @@ private void ThrowIfRunning()
564564
}
565565
}
566566

567+
bool IGlobalHookProvider.KeyTypedEnabled { get; set; }
568+
567569
ulong IEventSimulationProvider.PostTextDelayX11 { get; set; }
568570

569571
bool IAccessibilityProvider.PromptUserIfAxApiDisabled { get; set; } = true;

0 commit comments

Comments
 (0)