Repository navigation
Expand file tree
/
Copy pathNativeMethods.cs
More file actions
142 lines (124 loc) · 4.34 KB
/
Copy pathNativeMethods.cs
File metadata and controls
142 lines (124 loc) · 4.34 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
using System;
using System.Runtime.InteropServices;
namespace ReadEye
{
internal static class NativeMethods
{
// --- Prevent sleep API ---
[Flags]
public enum EXECUTION_STATE : uint
{
ES_AWAYMODE_REQUIRED = 0x00000040,
ES_CONTINUOUS = 0x80000000,
ES_DISPLAY_REQUIRED = 0x00000002,
ES_SYSTEM_REQUIRED = 0x00000001
}
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);
// --- Lid switch power notification API ---
public static readonly Guid GUID_LIDSWITCH_STATE_CHANGE = new Guid("BA3E0F4D-B817-4094-A2D1-D56379E6A0F3");
public const int WM_POWERBROADCAST = 0x0218;
public const int PBT_POWERSETTINGCHANGE = 0x8013;
public const int DEVICE_NOTIFY_WINDOW_HANDLE = 0x00000000;
[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct POWERBROADCAST_SETTING
{
public Guid PowerSetting;
public uint DataLength;
public byte Data;
}
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr RegisterPowerSettingNotification(IntPtr hRecipient, ref Guid PowerSettingGuid, int Flags);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnregisterPowerSettingNotification(IntPtr handle);
// --- Simulate input API (Jiggler mode fallback) ---
private const int INPUT_KEYBOARD = 1;
private const uint KEYEVENTF_KEYUP = 0x0002;
private const ushort VK_F15 = 0x7E; // Harmless function key F15
[StructLayout(LayoutKind.Sequential)]
private struct KEYBDINPUT
{
public ushort wVk;
public ushort wScan;
public uint dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
private struct MOUSEINPUT
{
public int dx;
public int dy;
public uint mouseData;
public uint dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
private struct HARDWAREINPUT
{
public uint uMsg;
public ushort wParamL;
public ushort wParamH;
}
[StructLayout(LayoutKind.Explicit)]
private struct INPUTUnion
{
[FieldOffset(0)]
public MOUSEINPUT mi;
[FieldOffset(0)]
public KEYBDINPUT ki;
[FieldOffset(0)]
public HARDWAREINPUT hi;
}
[StructLayout(LayoutKind.Sequential)]
private struct INPUT
{
public uint type;
public INPUTUnion u;
}
[DllImport("user32.dll", SetLastError = true)]
private static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);
/// <summary>
/// Simulates pressing and releasing the F15 key to reset the Windows idle timer.
/// </summary>
public static bool SimulateActivity()
{
INPUT[] inputs = new INPUT[2];
// Key down F15
inputs[0] = new INPUT
{
type = INPUT_KEYBOARD,
u = new INPUTUnion
{
ki = new KEYBDINPUT
{
wVk = VK_F15,
wScan = 0,
dwFlags = 0,
time = 0,
dwExtraInfo = IntPtr.Zero
}
}
};
// Key up F15
inputs[1] = new INPUT
{
type = INPUT_KEYBOARD,
u = new INPUTUnion
{
ki = new KEYBDINPUT
{
wVk = VK_F15,
wScan = 0,
dwFlags = KEYEVENTF_KEYUP,
time = 0,
dwExtraInfo = IntPtr.Zero
}
}
};
uint result = SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
return result == inputs.Length;
}
}
}