forked from E-riCA0/StawdewValley
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeyboardInput.cs
119 lines (107 loc) · 4.23 KB
/
KeyboardInput.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
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
// Decompiled with JetBrains decompiler
// Type: StardewValley.KeyboardInput
// Assembly: Stardew Valley, Version=1.2.6400.27469, Culture=neutral, PublicKeyToken=null
// MVID: 77B7094A-F6F0-4ACC-91F4-E335E2733EDB
// Assembly location: D:\SteamLibrary\steamapps\common\Stardew Valley\Stardew Valley.exe
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using System;
using System.Runtime.InteropServices;
namespace StardewValley
{
public static class KeyboardInput
{
private static bool initialized;
private static IntPtr prevWndProc;
private static KeyboardInput.WndProc hookProcDelegate;
private static IntPtr hIMC;
private const int GWL_WNDPROC = -4;
private const int WM_KEYDOWN = 256;
private const int WM_KEYUP = 257;
private const int WM_CHAR = 258;
private const int WM_IME_SETCONTEXT = 641;
private const int WM_INPUTLANGCHANGE = 81;
private const int WM_GETDLGCODE = 135;
private const int WM_IME_COMPOSITION = 271;
private const int DLGC_WANTALLKEYS = 4;
public static event CharEnteredHandler CharEntered;
public static event KeyEventHandler KeyDown;
public static event KeyEventHandler KeyUp;
[DllImport("Imm32.dll", CharSet = CharSet.Unicode)]
private static extern IntPtr ImmGetContext(IntPtr hWnd);
[DllImport("Imm32.dll", CharSet = CharSet.Unicode)]
private static extern IntPtr ImmAssociateContext(IntPtr hWnd, IntPtr hIMC);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern IntPtr CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
public static void Initialize(GameWindow window)
{
if (KeyboardInput.initialized)
throw new InvalidOperationException("TextInput.Initialize can only be called once!");
KeyboardInput.hookProcDelegate = new KeyboardInput.WndProc(KeyboardInput.HookProc);
KeyboardInput.prevWndProc = (IntPtr) KeyboardInput.SetWindowLong(window.Handle, -4, (int) Marshal.GetFunctionPointerForDelegate((Delegate) KeyboardInput.hookProcDelegate));
KeyboardInput.hIMC = KeyboardInput.ImmGetContext(window.Handle);
KeyboardInput.initialized = true;
}
private static IntPtr HookProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
{
IntPtr num = KeyboardInput.CallWindowProc(KeyboardInput.prevWndProc, hWnd, msg, wParam, lParam);
if (msg <= 135U)
{
if ((int) msg != 81)
{
if ((int) msg == 135)
num = (IntPtr) (num.ToInt32() | 4);
}
else
{
KeyboardInput.ImmAssociateContext(hWnd, KeyboardInput.hIMC);
num = (IntPtr) 1;
}
}
else
{
switch (msg)
{
case 256:
// ISSUE: reference to a compiler-generated field
if (KeyboardInput.KeyDown != null)
{
// ISSUE: reference to a compiler-generated field
KeyboardInput.KeyDown((object) null, new KeyEventArgs((Keys) (int) wParam));
break;
}
break;
case 257:
// ISSUE: reference to a compiler-generated field
if (KeyboardInput.KeyUp != null)
{
// ISSUE: reference to a compiler-generated field
KeyboardInput.KeyUp((object) null, new KeyEventArgs((Keys) (int) wParam));
break;
}
break;
case 258:
// ISSUE: reference to a compiler-generated field
if (KeyboardInput.CharEntered != null)
{
// ISSUE: reference to a compiler-generated field
KeyboardInput.CharEntered((object) null, new CharacterEventArgs((char) (int) wParam, lParam.ToInt32()));
break;
}
break;
case 641:
if (wParam.ToInt32() == 1)
{
KeyboardInput.ImmAssociateContext(hWnd, KeyboardInput.hIMC);
break;
}
break;
}
}
return num;
}
private delegate IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
}
}