1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using System . Runtime . InteropServices ;
4
- using System . Text ;
5
4
using System . Threading ;
6
5
7
6
using log4net ;
8
7
9
8
using PInvoke ;
10
9
10
+ using TwitchStreamingTools . Controls . ViewModels ;
11
11
using TwitchStreamingTools . Models ;
12
12
13
13
namespace TwitchStreamingTools . Services ;
14
14
15
15
/// <summary>
16
16
/// Detects when keys are pressed anywhere on the OS.
17
17
/// </summary>
18
- public class GlobalKeyPressService {
18
+ public class GlobalKeyPressService : IGlobalKeyPressService {
19
19
/// <summary>
20
20
/// The logger.
21
21
/// </summary>
@@ -53,6 +53,11 @@ public class GlobalKeyPressService {
53
53
/// </summary>
54
54
private readonly Thread _thread ;
55
55
56
+ /// <summary>
57
+ /// The keystroke callback.
58
+ /// </summary>
59
+ private Action < Keybind > ? s_onKeystroke ;
60
+
56
61
/// <summary>
57
62
/// Initializes a new instance of the <see cref="GlobalKeyPressService" /> class.
58
63
/// </summary>
@@ -67,7 +72,15 @@ public GlobalKeyPressService() {
67
72
/// <summary>
68
73
/// Gets or sets the callbacks to invoke when a keystroke is pressed.
69
74
/// </summary>
70
- public static Action < Keys , bool , bool , bool > ? OnKeystroke { get ; set ; }
75
+ public Action < Keybind > ? OnKeystroke {
76
+ get => s_onKeystroke ;
77
+ set => s_onKeystroke = value ;
78
+ }
79
+
80
+ /// <inheritdoc />
81
+ public bool IsModifier ( Keys key ) {
82
+ return modifiers . Contains ( key ) ;
83
+ }
71
84
72
85
/// <summary>
73
86
/// The main loop which registers for keystrokes on the system and flushes the message buffer.
@@ -90,7 +103,7 @@ private void Main() {
90
103
/// <param name="wParam">The <seealso cref="KeyboardMessage" />.</param>
91
104
/// <param name="lParam">The <seealso cref="KeyboardLowLevelHookStruct" />.</param>
92
105
/// <returns>The next hook that should be called.</returns>
93
- private static int KeystrokeCallback ( int nCode , IntPtr wParam , IntPtr lParam ) {
106
+ private int KeystrokeCallback ( int nCode , IntPtr wParam , IntPtr lParam ) {
94
107
var keyboardEvent = Marshal . PtrToStructure < KeyboardLowLevelHookStruct > ( lParam ) ;
95
108
var whatHappened = ( KeyboardMessage ) wParam ;
96
109
var key = ( Keys ) keyboardEvent . vkCode ;
@@ -104,8 +117,14 @@ private static int KeystrokeCallback(int nCode, IntPtr wParam, IntPtr lParam) {
104
117
bool holdingAlt = ( keyboardEvent . flags & 0b_0001_0000 ) != 0 ;
105
118
bool holdingCtrl = holding [ Keys . Control ] || holding [ Keys . ControlKey ] || holding [ Keys . LControlKey ] || holding [ Keys . RControlKey ] ;
106
119
bool holdingShift = holding [ Keys . Shift ] || holding [ Keys . ShiftKey ] || holding [ Keys . LShiftKey ] || holding [ Keys . RShiftKey ] ;
107
- LogKey ( key , holdingCtrl , holdingAlt , holdingShift ) ;
108
- OnKeystroke ? . Invoke ( key , holdingCtrl , holdingAlt , holdingShift ) ;
120
+ var keybind = new Keybind {
121
+ IsCtrl = holdingCtrl ,
122
+ IsShift = holdingShift ,
123
+ IsAlt = holdingAlt ,
124
+ Key = key
125
+ } ;
126
+ LogKey ( keybind ) ;
127
+ s_onKeystroke ? . Invoke ( keybind ) ;
109
128
}
110
129
else if ( whatHappened == KeyboardMessage . KEY_UP ) {
111
130
if ( modifiers . Contains ( key ) ) {
@@ -119,26 +138,8 @@ private static int KeystrokeCallback(int nCode, IntPtr wParam, IntPtr lParam) {
119
138
/// <summary>
120
139
/// Logs the keystroke for debugging.
121
140
/// </summary>
122
- /// <param name="key">The key pressed.</param>
123
- /// <param name="holdingCtrl">True if control is held down.</param>
124
- /// <param name="holdingAlt">True if alt is held down.</param>
125
- /// <param name="holdingShift">True if shift is held down.</param>
126
- private static void LogKey ( Keys key , bool holdingCtrl , bool holdingAlt , bool holdingShift ) {
127
- var sb = new StringBuilder ( "Key Pressed: " ) ;
128
- if ( holdingCtrl ) {
129
- sb . Append ( "Ctrl + " ) ;
130
- }
131
-
132
- if ( holdingShift ) {
133
- sb . Append ( "Shift + " ) ;
134
- }
135
-
136
- if ( holdingAlt ) {
137
- sb . Append ( "Alt + " ) ;
138
- }
139
-
140
- sb . Append ( key ) ;
141
-
142
- LOGGER . Debug ( sb . ToString ( ) ) ;
141
+ /// <param name="keybind">The key pressed.</param>
142
+ private void LogKey ( Keybind keybind ) {
143
+ LOGGER . Debug ( $ "Key pressed: { keybind } ") ;
143
144
}
144
145
}
0 commit comments