This repository was archived by the owner on Dec 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathNativeMethods.cs
More file actions
95 lines (83 loc) · 3.31 KB
/
NativeMethods.cs
File metadata and controls
95 lines (83 loc) · 3.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
namespace MemcachedService64
{
static class NativeMethods
{
#region IsWow64Process
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public extern static bool IsWow64Process(IntPtr hProcess, [MarshalAs(UnmanagedType.Bool)] out bool isWow64);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public extern static IntPtr GetCurrentProcess();
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
public extern static IntPtr GetModuleHandle(string moduleName);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public extern static IntPtr GetProcAddress(IntPtr hModule, string methodName);
#endregion
#region PeekEscapeKey
// Source: http://stackoverflow.com/questions/10342392/intercept-esc-without-removing-other-key-presses-from-buffer
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
internal struct InputRecord
{
internal short eventType;
internal KeyEventRecord keyEvent;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
internal struct KeyEventRecord
{
internal bool keyDown;
internal short repeatCount;
internal short virtualKeyCode;
internal short virtualScanCode;
internal char uChar;
internal int controlKeyState;
}
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern bool PeekConsoleInput(IntPtr hConsoleInput, out InputRecord buffer, int numInputRecords_UseOne, out int numEventsRead);
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern bool ReadConsoleInput(IntPtr hConsoleInput, out InputRecord buffer, int numInputRecords_UseOne, out int numEventsRead);
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern IntPtr GetStdHandle(int nStdHandle);
static Nullable<InputRecord> PeekConsoleEvent()
{
InputRecord ir;
int num;
if (PeekConsoleInput(GetStdHandle(-10), out ir, 1, out num) && num != 0)
return ir;
return null;
}
static InputRecord ReadConsoleInput()
{
InputRecord ir;
int num;
ReadConsoleInput(GetStdHandle(-10), out ir, 1, out num);
return ir;
}
public static bool PeekEscapeKey()
{
for (; ; )
{
var ev = PeekConsoleEvent();
if (ev == null)
{
Thread.Sleep(10);
continue;
}
if (ev.Value.eventType == 1 && ev.Value.keyEvent.keyDown && ev.Value.keyEvent.virtualKeyCode == 27)
{
ReadConsoleInput();
return true;
}
if (ev.Value.eventType == 1 && ev.Value.keyEvent.keyDown)
return false;
ReadConsoleInput();
}
}
#endregion
}
}