Repository navigation
Expand file tree
/
Copy pathKeyboard.vb
More file actions
188 lines (132 loc) · 5.31 KB
/
Copy pathKeyboard.vb
File metadata and controls
188 lines (132 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
'' Copyright© Codepotro.
'' This file Is part Of HelloIME.
'' HelloIME Is free software: you can redistribute it And/Or modify
'' it under the terms Of the GNU General Public License As published by
'' the Free Software Foundation, either version 3 Of the License, Or
'' (at your option) any later version.
'' HelloIME Is distributed In the hope that it will be useful,
'' but WITHOUT ANY WARRANTY; without even the implied warranty of
'' MERCHANTABILITY Or FITNESS FOR A PARTICULAR PURPOSE. See the
'' GNU General Public License For more details.
'' You should have received a copy Of the GNU General Public License
'' along with HelloIME. If Not, see < https: //www.gnu.org/licenses/>.
Imports System.Runtime.InteropServices
Imports System.Reflection
Imports System.Drawing
Imports System.Threading
Module Keyboard
Public Declare Function UnhookWindowsHookEx Lib "user32" _
(ByVal hHook As Integer) As Integer
Public Declare Function SetWindowsHookEx Lib "user32" _
Alias "SetWindowsHookExA" (ByVal idHook As Integer,
ByVal lpfn As KeyboardHookDelegate, ByVal hmod As Integer,
ByVal dwThreadId As Integer) As Integer
Private Declare Function GetAsyncKeyState Lib "user32" _
(ByVal vKey As Integer) As Integer
Private Declare Function CallNextHookEx Lib "user32" _
(ByVal hHook As Integer,
ByVal nCode As Integer,
ByVal wParam As Integer,
ByVal lParam As KBDLLHOOKSTRUCT) As Integer
Public Structure KBDLLHOOKSTRUCT
Public vkCode As Integer
Public scanCode As Integer
Public flags As Integer
Public time As Integer
Public dwExtraInfo As Integer
End Structure
' Low-Level Keyboard Constants
Private Const HC_ACTION As Integer = 0
Private Const LLKHF_EXTENDED As Integer = &H1
Private Const LLKHF_INJECTED As Integer = &H10
Private Const LLKHF_ALTDOWN As Integer = &H20
Private Const LLKHF_UP As Integer = &H80
Private Const WM_KEYDOWN As Integer = 256
' Virtual Keys
Public Const VK_TAB = &H9
Public Const VK_CONTROL = &H11
Public Const VK_ESCAPE = &H1B
Public Const VK_DELETE = &H2E
Private Const WH_KEYBOARD_LL As Integer = 13&
Public KeyboardHandle As Integer
Public Function IsHooked(
ByRef Hookstruct As KBDLLHOOKSTRUCT) As Boolean
Debug.WriteLine("Hookstruct.vkCode: " & Hookstruct.vkCode)
If (Hookstruct.vkCode = 65) Then
SendKey("আ")
Return True
End If
''Here 65 is the code for Character "A"
''SendKey("") sends the string if the key("A") is pressed
''"Return True" blocks input "A"
Return False
End Function
Private Sub HookedState(ByVal Text As String)
Debug.WriteLine(Text)
End Sub
Public Function KeyboardCallback(ByVal Code As Integer,
ByVal wParam As Integer,
ByRef lParam As KBDLLHOOKSTRUCT) As Integer
If Code = HC_ACTION And wParam = WM_KEYDOWN Then
Debug.WriteLine("Calling IsHooked")
If (IsHooked(lParam)) Then
Return 1
End If
End If
Return CallNextHookEx(KeyboardHandle,
Code, wParam, lParam)
End Function
Public Delegate Function KeyboardHookDelegate(
ByVal Code As Integer,
ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) _
As Integer
<MarshalAs(UnmanagedType.FunctionPtr)>
Private callback As KeyboardHookDelegate
Public Sub HookKeyboard()
callback = New KeyboardHookDelegate(AddressOf KeyboardCallback)
KeyboardHandle = SetWindowsHookEx(
WH_KEYBOARD_LL, callback,
Marshal.GetHINSTANCE(
[Assembly].GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
Call CheckHooked()
End Sub
Public Sub CheckHooked()
If (Hooked()) Then
Debug.WriteLine("Keyboard successfully hooked")
Else
Debug.WriteLine("Keyboard hooking failed: " & Err.LastDllError)
End If
End Sub
Private Function Hooked()
Hooked = KeyboardHandle <> 0
End Function
Public Sub UnhookKeyboard()
If (Hooked()) Then
Call UnhookWindowsHookEx(KeyboardHandle)
End If
End Sub
Private Sub DoKeyBoard(ByVal flags As NativeMethods.KEYEVENTF, ByVal key As Keys)
Dim input As New NativeMethods.INPUT
Dim ki As New NativeMethods.KEYBDINPUT
input.dwType = NativeMethods.InputType.Keyboard
input.ki = ki
input.ki.wVk = 0
input.ki.wScan = Convert.ToInt16(key)
input.ki.time = 0
input.ki.dwFlags = flags
input.ki.dwExtraInfo = IntPtr.Zero
Dim cbSize As Integer = Marshal.SizeOf(GetType(NativeMethods.INPUT))
Dim result As Integer = NativeMethods.SendInput(1, input, cbSize)
If result = 0 Then Debug.WriteLine(Marshal.GetLastWin32Error)
End Sub
Private Sub SendKey(ByVal Text As String)
Dim length As Integer = Text.Length
For i As Integer = 1 To length Step 1
SendKeyByte(CShort(Strings.AscW(Strings.Mid(Text, i, 1))))
Next
End Sub
Private Sub SendKeyByte(ByVal KeyCode As Short)
DoKeyBoard(NativeMethods.KEYEVENTF.UNICODE, KeyCode)
DoKeyBoard(NativeMethods.KEYEVENTF.KEYUP, KeyCode)
End Sub
End Module