Repository navigation
Expand file tree
/
Copy pathNative.cs
More file actions
109 lines (83 loc) · 4.61 KB
/
Copy pathNative.cs
File metadata and controls
109 lines (83 loc) · 4.61 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
using System;
using System.Runtime.InteropServices;
namespace WinTextCapture;
/// <summary>All Win32 interop in one place.</summary>
internal static class Native
{
// ---- low-level keyboard hook ----
public const int WH_KEYBOARD_LL = 13;
public const int WM_KEYDOWN = 0x0100;
public const int WM_SYSKEYDOWN = 0x0104;
public const int WM_KEYUP = 0x0101;
public const int WM_SYSKEYUP = 0x0105;
public delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr GetModuleHandle(string? lpModuleName);
[DllImport("user32.dll")]
public static extern short GetAsyncKeyState(int vKey);
// virtual-key codes used for modifier polling
public const int VK_SHIFT = 0x10, VK_CONTROL = 0x11, VK_MENU = 0x12, VK_LWIN = 0x5B, VK_RWIN = 0x5C, VK_LCONTROL = 0xA2;
public static bool IsDown(int vk) => (GetAsyncKeyState(vk) & 0x8000) != 0;
// ---- SendInput (used to mask the Win key so it doesn't open the Start menu) ----
public const uint INPUT_KEYBOARD = 1;
public const uint KEYEVENTF_KEYUP = 0x0002;
// sentinel stamped on our own injected input so we can recognise/ignore it
public static readonly IntPtr InjectedTag = (IntPtr)0x57544558; // "WTEX"
[StructLayout(LayoutKind.Sequential)]
public struct MOUSEINPUT { public int dx, dy; public uint mouseData, dwFlags, time; public IntPtr dwExtraInfo; }
[StructLayout(LayoutKind.Sequential)]
public struct KEYBDINPUT { public ushort wVk, wScan; public uint dwFlags, time; public IntPtr dwExtraInfo; }
[StructLayout(LayoutKind.Explicit)]
public struct INPUTUNION
{
[FieldOffset(0)] public MOUSEINPUT mi;
[FieldOffset(0)] public KEYBDINPUT ki;
}
[StructLayout(LayoutKind.Sequential)]
public struct INPUT { public uint type; public INPUTUNION u; }
[DllImport("user32.dll", SetLastError = true)]
public static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);
/// <summary>
/// Tap Left-Ctrl (down+up). When a Win-key hotkey is consumed, the trailing lone Win key-up would
/// otherwise pop the Start menu; injecting a masking key while Win is held cancels that.
/// </summary>
public static void MaskWinKey()
{
var inputs = new INPUT[2];
inputs[0].type = INPUT_KEYBOARD;
inputs[0].u.ki = new KEYBDINPUT { wVk = VK_LCONTROL, dwExtraInfo = InjectedTag };
inputs[1].type = INPUT_KEYBOARD;
inputs[1].u.ki = new KEYBDINPUT { wVk = VK_LCONTROL, dwFlags = KEYEVENTF_KEYUP, dwExtraInfo = InjectedTag };
SendInput(2, inputs, Marshal.SizeOf<INPUT>());
}
// ---- virtual screen metrics (physical pixels in a PerMonitorV2 process) ----
public const int SM_XVIRTUALSCREEN = 76, SM_YVIRTUALSCREEN = 77, SM_CXVIRTUALSCREEN = 78, SM_CYVIRTUALSCREEN = 79;
[DllImport("user32.dll")]
public static extern int GetSystemMetrics(int nIndex);
public static readonly IntPtr DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 = new(-4);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetProcessDpiAwarenessContext(IntPtr value);
// ---- window positioning / focus ----
public static readonly IntPtr HWND_TOPMOST = new(-1);
public const uint SWP_SHOWWINDOW = 0x0040;
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
// ---- dark title bar ----
public const int DWMWA_USE_IMMERSIVE_DARK_MODE = 20;
public const int DWMWA_WINDOW_CORNER_PREFERENCE = 33; // Windows 11; ignored on Windows 10
public const int DWMWCP_ROUND = 2, DWMWCP_ROUNDSMALL = 3;
[DllImport("dwmapi.dll")]
public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int value, int size);
}