-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataSource.cs
74 lines (64 loc) · 2.87 KB
/
DataSource.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Input;
using System.Diagnostics;
using System.Windows;
namespace KeyBinder
{
/// <summary>
/// This class serves as a data source for keylogger. It provides method to get what we want.
/// </summary>
public class DataSource
{
[DllImport("User32.dll")]
private static extern short GetAsyncKeyState(int vKey);
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", SetLastError = true)]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId);
private HashSet<Key> PressedKeys = new HashSet<Key>();
private static readonly Key[] INVALID_KEYS = new Key[] { Key.Back, Key.Enter, Key.LWin, Key.Apps, Key.Escape, Key.CapsLock,
Key.LineFeed, Key.System, Key.DeadCharProcessed };
/// <summary>
/// This functions scans currently pressed keys and returns them. Every key is returned just once. If the key is still pressed during second
/// method call, it is not returned. It's returned again after the key is released and pressed again.
/// </summary>
/// <returns>List of keys which were just pressed</returns>
public HashSet<Key> GetPressedKeys()
{
// Get state of every key we know
foreach (Key key in Utils.GetEnumValues<Key>().Where(x => x != Key.None))
{
bool down = false;
// Is it pressed?
Application.Current.Dispatcher.Invoke((Action)delegate {
down = Keyboard.IsKeyDown(key);
});
// It's not pressed, but it was - we consider this key as released
if (!down && PressedKeys.Contains(key))
{
PressedKeys.Remove(key);
}
else if (down && !PressedKeys.Contains(key) && !INVALID_KEYS.Contains(key)) // The key is pressed, but wasn't pressed before - it will be returned
{
PressedKeys.Add(key);
}
}
return PressedKeys;
}
/// <summary>
/// Search for currently active window (focused) and returns name of the process of that window.
/// So if user is using Chrome right now, 'chrome' string will be returned.
/// </summary>
/// <returns>Name of the process who is tied to currently active window</returns>
public string GetActiveWindowProcessName()
{
IntPtr windowHandle = GetForegroundWindow();
GetWindowThreadProcessId(windowHandle, out uint processId);
Process process = Process.GetProcessById((int)processId);
return process.ProcessName;
}
}
}