mirror: 上游快照导入
This commit is contained in:
commit
7475ccb467
86 files changed
+3213
No files matched your search
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using SharpDX.XInput;
|
||||
|
||||
namespace CSharpModBase.Input
|
||||
{
|
||||
[Flags]
|
||||
public enum GamePadButton
|
||||
{
|
||||
None = GamepadButtonFlags.None,
|
||||
DPadUp = GamepadButtonFlags.DPadUp,
|
||||
DPadDown = GamepadButtonFlags.DPadDown,
|
||||
DPadLeft = GamepadButtonFlags.DPadLeft,
|
||||
DPadRight = GamepadButtonFlags.DPadRight,
|
||||
Start = GamepadButtonFlags.Start,
|
||||
Back = GamepadButtonFlags.Back,
|
||||
LeftThumb = GamepadButtonFlags.LeftThumb,
|
||||
RightThumb = GamepadButtonFlags.RightThumb,
|
||||
LeftShoulder = GamepadButtonFlags.LeftShoulder,
|
||||
RightShoulder = GamepadButtonFlags.RightShoulder,
|
||||
A = GamepadButtonFlags.A,
|
||||
B = GamepadButtonFlags.B,
|
||||
X = GamepadButtonFlags.X,
|
||||
Y = GamepadButtonFlags.Y,
|
||||
LeftTrigger = 0x10000,
|
||||
RightTrigger = 0x20000,
|
||||
DPadRightUp = DPadRight | DPadUp,
|
||||
DPadLeftDown = DPadLeft | DPadDown,
|
||||
DPadRightDown = DPadRight | DPadDown,
|
||||
DPadLeftUp = DPadLeft | DPadUp,
|
||||
}
|
||||
|
||||
|
||||
public static class GamePadUtils
|
||||
{
|
||||
private static readonly Controller controller = new Controller(UserIndex.One);
|
||||
|
||||
public static bool GetGamePadButtons(out GamePadButton flags)
|
||||
{
|
||||
flags = GamePadButton.None;
|
||||
if (controller.GetState(out State state))
|
||||
{
|
||||
Gamepad gamepad = state.Gamepad;
|
||||
flags = (GamePadButton)gamepad.Buttons;
|
||||
if (gamepad.LeftTrigger > 100) flags |= GamePadButton.LeftTrigger;
|
||||
if (gamepad.RightTrigger > 100) flags |= GamePadButton.RightTrigger;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class GamePadButtonEvent : EventArgs
|
||||
{
|
||||
public GamePadButton Button { get; set; }
|
||||
}
|
||||
|
||||
|
||||
public delegate void GamePadButtonEventHandler(GamePadButtonEvent e);
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
using System;
|
||||
|
||||
namespace CSharpModBase.Input
|
||||
{
|
||||
public class HotKeyData
|
||||
{
|
||||
public HotKeyData(ModifierKeys modifiers, Key key)
|
||||
{
|
||||
Modifiers = modifiers;
|
||||
Key = key;
|
||||
}
|
||||
|
||||
public HotKeyData() : this(ModifierKeys.None, Key.None) {}
|
||||
|
||||
public ModifierKeys Modifiers { get; set; }
|
||||
public Key Key { get; set; }
|
||||
public GamePadButton GamePadButton { get; set; }
|
||||
|
||||
public bool IsValid => Key != Key.None || GamePadButton != GamePadButton.None;
|
||||
public string KeyString => KeyUtils.KeyToString(Modifiers, Key);
|
||||
|
||||
public static uint GetCode(ModifierKeys modifiers, Key key)
|
||||
{
|
||||
return ((uint)modifiers << 16) | (uint)key;
|
||||
}
|
||||
|
||||
public uint Code
|
||||
{
|
||||
get
|
||||
{
|
||||
if (GamePadButton != GamePadButton.None)
|
||||
{
|
||||
return 0xF0000000 | (uint)GamePadButton;
|
||||
}
|
||||
else
|
||||
{
|
||||
return GetCode(Modifiers, Key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void SetFromCode(uint code)
|
||||
{
|
||||
if ((code >> 24) == 0xF0)
|
||||
{
|
||||
GamePadButton = (GamePadButton)(code & 0xFFFF);
|
||||
Modifiers = ModifierKeys.None;
|
||||
Key = Key.None;
|
||||
}
|
||||
else
|
||||
{
|
||||
GamePadButton = GamePadButton.None;
|
||||
Modifiers = (ModifierKeys)(code >> 16);
|
||||
Key = (Key)(code & 0xFF);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetKey(ModifierKeys modifiers, Key key)
|
||||
{
|
||||
Modifiers = modifiers;
|
||||
Key = key;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return KeyString;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class HotKeyItem : HotKeyData
|
||||
{
|
||||
public string Label { get; set; }
|
||||
public Action Action { get; set; }
|
||||
/// <summary>
|
||||
/// 0为不支持连按,大于0为连按触发毫秒间隔
|
||||
/// </summary>
|
||||
public int RepeatMs { get; set; }
|
||||
/// <summary>
|
||||
/// 上一次触发的时间,毫秒
|
||||
/// </summary>
|
||||
public long LastTriggerMs { get; set; }
|
||||
/// <summary>
|
||||
/// 正在被按下
|
||||
/// </summary>
|
||||
public bool IsPressed { get; set; }
|
||||
public bool RunOnGameThread { get; set; } = true;
|
||||
|
||||
public HotKeyItem WithKey(ModifierKeys Modifiers, Key Key)
|
||||
{
|
||||
return new(Label, Modifiers, Key, Action);
|
||||
}
|
||||
|
||||
public HotKeyItem(ModifierKeys modifiers, Key key, Action action) : this("", modifiers, key, action)
|
||||
{
|
||||
}
|
||||
|
||||
public HotKeyItem(string label, ModifierKeys modifiers, Key key, Action action) : base(modifiers, key)
|
||||
{
|
||||
Label = label;
|
||||
Action = action;
|
||||
}
|
||||
|
||||
public HotKeyItem() : this("", ModifierKeys.None, Key.None, EmptyAction)
|
||||
{
|
||||
}
|
||||
|
||||
private static void EmptyAction()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
|
||||
namespace CSharpModBase.Input
|
||||
{
|
||||
public interface IInputManager
|
||||
{
|
||||
HotKeyItem RegisterKeyBind(Key key, Action action);
|
||||
|
||||
HotKeyItem RegisterKeyBind(ModifierKeys modifiers, Key key, Action action);
|
||||
HotKeyItem RegisterGamePadBind(GamePadButton button, Action action);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
namespace CSharpModBase.Input
|
||||
{
|
||||
public enum Key
|
||||
{
|
||||
None = 0x00,
|
||||
LBUTTON = 0x01,
|
||||
RBUTTON = 0x02,
|
||||
CANCEL = 0x03,
|
||||
MBUTTON = 0x04,
|
||||
XBUTTON1 = 0x05,
|
||||
XBUTTON2 = 0x06,
|
||||
BACK = 0x08,
|
||||
TAB = 0x09,
|
||||
CLEAR = 0x0C,
|
||||
RETURN = 0x0D,
|
||||
ENTER = RETURN,
|
||||
SHIFT = 0x10,
|
||||
CONTROL = 0x11,
|
||||
MENU = 0x12,
|
||||
PAUSE = 0x13,
|
||||
CAPITAL = 0x14,
|
||||
KANA = 0x15,
|
||||
HANGEUL = 0x15,
|
||||
HANGUL = 0x15,
|
||||
ESCAPE = 0x1B,
|
||||
CONVERT = 0x1C,
|
||||
NONCONVERT = 0x1D,
|
||||
ACCEPT = 0x1E,
|
||||
MODECHANGE = 0x1F,
|
||||
SPACE = 0x20,
|
||||
PRIOR = 0x21,
|
||||
NEXT = 0x22,
|
||||
END = 0x23,
|
||||
HOME = 0x24,
|
||||
LEFT = 0x25,
|
||||
UP = 0x26,
|
||||
RIGHT = 0x27,
|
||||
DOWN = 0x28,
|
||||
SELECT = 0x29,
|
||||
PRINT = 0x2A,
|
||||
EXECUTE = 0x2B,
|
||||
SNAPSHOT = 0x2C,
|
||||
INSERT = 0x2D,
|
||||
DELETE = 0x2E,
|
||||
HELP = 0x2F,
|
||||
D0 = 0x30,
|
||||
D1 = 0x31,
|
||||
D2 = 0x32,
|
||||
D3 = 0x33,
|
||||
D4 = 0x34,
|
||||
D5 = 0x35,
|
||||
D6 = 0x36,
|
||||
D7 = 0x37,
|
||||
D8 = 0x38,
|
||||
D9 = 0x39,
|
||||
A = 0x41,
|
||||
B = 0x42,
|
||||
C = 0x43,
|
||||
D = 0x44,
|
||||
E = 0x45,
|
||||
F = 0x46,
|
||||
G = 0x47,
|
||||
H = 0x48,
|
||||
I = 0x49,
|
||||
J = 0x4A,
|
||||
K = 0x4B,
|
||||
L = 0x4C,
|
||||
M = 0x4D,
|
||||
N = 0x4E,
|
||||
O = 0x4F,
|
||||
P = 0x50,
|
||||
Q = 0x51,
|
||||
R = 0x52,
|
||||
S = 0x53,
|
||||
T = 0x54,
|
||||
U = 0x55,
|
||||
V = 0x56,
|
||||
W = 0x57,
|
||||
X = 0x58,
|
||||
Y = 0x59,
|
||||
Z = 0x5A,
|
||||
LWIN = 0x5B,
|
||||
RWIN = 0x5C,
|
||||
APPS = 0x5D,
|
||||
SLEEP = 0x5F,
|
||||
NUMPAD0 = 0x60,
|
||||
NUMPAD1 = 0x61,
|
||||
NUMPAD2 = 0x62,
|
||||
NUMPAD3 = 0x63,
|
||||
NUMPAD4 = 0x64,
|
||||
NUMPAD5 = 0x65,
|
||||
NUMPAD6 = 0x66,
|
||||
NUMPAD7 = 0x67,
|
||||
NUMPAD8 = 0x68,
|
||||
NUMPAD9 = 0x69,
|
||||
MULTIPLY = 0x6A,
|
||||
ADD = 0x6B,
|
||||
SEPARATOR = 0x6C,
|
||||
SUBTRACT = 0x6D,
|
||||
DECIMAL = 0x6E,
|
||||
DIVIDE = 0x6F,
|
||||
F1 = 0x70,
|
||||
F2 = 0x71,
|
||||
F3 = 0x72,
|
||||
F4 = 0x73,
|
||||
F5 = 0x74,
|
||||
F6 = 0x75,
|
||||
F7 = 0x76,
|
||||
F8 = 0x77,
|
||||
F9 = 0x78,
|
||||
F10 = 0x79,
|
||||
F11 = 0x7A,
|
||||
F12 = 0x7B,
|
||||
F13 = 0x7C,
|
||||
F14 = 0x7D,
|
||||
F15 = 0x7E,
|
||||
F16 = 0x7F,
|
||||
F17 = 0x80,
|
||||
F18 = 0x81,
|
||||
F19 = 0x82,
|
||||
F20 = 0x83,
|
||||
F21 = 0x84,
|
||||
F22 = 0x85,
|
||||
F23 = 0x86,
|
||||
F24 = 0x87,
|
||||
NUMLOCK = 0x90,
|
||||
SCROLL = 0x91,
|
||||
LSHIFT = 0xA0,
|
||||
RSHIFT = 0xA1,
|
||||
LCONTROL = 0xA2,
|
||||
RCONTROL = 0xA3,
|
||||
LMENU = 0xA4,
|
||||
RMENU = 0xA5,
|
||||
OEM_1 = 0xBA, // ';'
|
||||
OEM_PLUS = 0xBB, // '+'
|
||||
OEM_COMMA = 0xBC, // ','
|
||||
OEM_MINUS = 0xBD, // '-'
|
||||
OEM_PERIOD = 0xBE, // '.'
|
||||
OEM_2 = 0xBF, // '/'
|
||||
OEM_3 = 0xC0, // '`'
|
||||
OEM_4 = 0xDB, // '['
|
||||
OEM_5 = 0xDC, // '\\'
|
||||
OEM_6 = 0xDD, // ']'
|
||||
OEM_7 = 0xDE, // '\''
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace CSharpModBase.Input
|
||||
{
|
||||
public static class KeyUtils
|
||||
{
|
||||
/// <summary>
|
||||
/// Keys转字符串,支持组合键
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public static string KeyToString(ModifierKeys modifiers, Key key, List<string> buf)
|
||||
{
|
||||
if (modifiers.HasFlag(ModifierKeys.Control)) buf.Add("Ctrl");
|
||||
if (modifiers.HasFlag(ModifierKeys.Shift)) buf.Add("Shift");
|
||||
if (modifiers.HasFlag(ModifierKeys.Alt)) buf.Add("Alt");
|
||||
string? keyCodeStr = null;
|
||||
char oemKeyChar = OemKeyChar(key);
|
||||
if (oemKeyChar != default) {
|
||||
keyCodeStr = oemKeyChar.ToString();
|
||||
} else {
|
||||
keyCodeStr = key switch
|
||||
{
|
||||
Key.None => "",
|
||||
_ => key.ToString(),
|
||||
};
|
||||
}
|
||||
if (keyCodeStr != null)
|
||||
{
|
||||
buf.Add(keyCodeStr);
|
||||
}
|
||||
return string.Join("+", buf);
|
||||
}
|
||||
|
||||
public static string KeyToString(ModifierKeys modifiers, Key key)
|
||||
{
|
||||
return KeyToString(modifiers, key, new());
|
||||
}
|
||||
|
||||
public static char OemKeyChar(Key key)
|
||||
{
|
||||
return key switch
|
||||
{
|
||||
Key.OEM_PLUS => '+',
|
||||
Key.OEM_COMMA => ',',
|
||||
Key.OEM_MINUS => '-',
|
||||
Key.OEM_PERIOD => '.',
|
||||
Key.OEM_1 => ';',
|
||||
Key.OEM_2 => '/',
|
||||
Key.OEM_3 => '`',
|
||||
Key.OEM_4 => '[',
|
||||
Key.OEM_5 => '\\',
|
||||
Key.OEM_6 => ']',
|
||||
Key.OEM_7 => '\'',
|
||||
_ => default,
|
||||
};
|
||||
}
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern short GetKeyState(uint vk);
|
||||
|
||||
public static bool IsKeyDown(Key key)
|
||||
{
|
||||
return GetKeyState((uint)key) < 0;
|
||||
}
|
||||
|
||||
public static ModifierKeys Modifiers
|
||||
{
|
||||
get
|
||||
{
|
||||
ModifierKeys modifiers = ModifierKeys.None;
|
||||
if (GetKeyState((uint)Key.MENU) < 0)
|
||||
{
|
||||
modifiers |= ModifierKeys.Alt;
|
||||
}
|
||||
if (GetKeyState((uint)Key.CONTROL) < 0)
|
||||
{
|
||||
modifiers |= ModifierKeys.Control;
|
||||
}
|
||||
if (GetKeyState((uint)Key.SHIFT) < 0)
|
||||
{
|
||||
modifiers |= ModifierKeys.Shift;
|
||||
}
|
||||
return modifiers;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
|
||||
namespace CSharpModBase.Input
|
||||
{
|
||||
[Flags]
|
||||
public enum ModifierKeys
|
||||
{
|
||||
None = 0,
|
||||
Alt = 1,
|
||||
Control = 2,
|
||||
Shift = 4,
|
||||
Windows = 8
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user