HKMBFZ/Assets/Scripts/Szz_Scripts/MyKeyBend.cs

198 lines
6.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
/// <summary>
/// 给每个按钮都绑定一个映射按键,模拟真实输入
/// </summary>
public class MyKeyBend : MonoBehaviour
{
public KeyType keyType; // 按键类型
public bool enableHold = false; // 是否长按
public float holdInterval = 0.1f; // 长按间隔
private bool isHolding = false;
// 引入系统 keybd_event
[DllImport("user32.dll", EntryPoint = "keybd_event")]
public static extern void Keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
private const int KEYEVENTF_KEYDOWN = 0;
private const int KEYEVENTF_KEYUP = 2;
private void Start()
{
enableHold = true;
}
void OnMouseDown()
{
}
public void OnKeyPress()
{
Debug.Log("KeyPress00"+ enableHold);
if (enableHold)
{
isHolding = true;
Debug.Log("KeyPress33");
StartCoroutine(HoldRoutine());
}
}
//void OnMouseUp()
//{
// isHolding = false;
//}
private IEnumerator HoldRoutine()
{
Debug.Log("KeyPress11");
while (isHolding)
{
yield return new WaitForSeconds(holdInterval);
PressKey();
}
}
private void PressKey()
{
// 保证焦点在当前选中对象
//GameObject selected = EventSystem.current.currentSelectedGameObject;
//if (selected != null)
//{
// TMP_InputField tmp = selected.GetComponent<TMP_InputField>();
// InputField input = selected.GetComponent<InputField>();
// if (tmp != null || input != null)
// {
// EventSystem.current.SetSelectedGameObject(selected);
// if (tmp != null) tmp.ActivateInputField();
// if (input != null) input.ActivateInputField();
// }
//}
Debug.Log("KeyPress");
// 发送系统按键事件
byte vk = KeyTypeToVK(keyType);
if (vk == 0) return;
Keybd_event(vk, 0, KEYEVENTF_KEYDOWN, 0);
Keybd_event(vk, 0, KEYEVENTF_KEYUP, 0);
isHolding = false;
}
private byte KeyTypeToVK(KeyType type)
{
switch (type)
{
// 字母
case KeyType.A: return 65;
case KeyType.B: return 66;
case KeyType.C: return 67;
case KeyType.D: return 68;
case KeyType.E: return 69;
case KeyType.F: return 70;
case KeyType.G: return 71;
case KeyType.H: return 72;
case KeyType.I: return 73;
case KeyType.J: return 74;
case KeyType.K: return 75;
case KeyType.L: return 76;
case KeyType.M: return 77;
case KeyType.N: return 78;
case KeyType.O: return 79;
case KeyType.P: return 80;
case KeyType.Q: return 81;
case KeyType.R: return 82;
case KeyType.S: return 83;
case KeyType.T: return 84;
case KeyType.U: return 85;
case KeyType.V: return 86;
case KeyType.W: return 87;
case KeyType.X: return 88;
case KeyType.Y: return 89;
case KeyType.Z: return 90;
// 数字
case KeyType.Digit0: return 48;
case KeyType.Digit1: return 49;
case KeyType.Digit2: return 50;
case KeyType.Digit3: return 51;
case KeyType.Digit4: return 52;
case KeyType.Digit5: return 53;
case KeyType.Digit6: return 54;
case KeyType.Digit7: return 55;
case KeyType.Digit8: return 56;
case KeyType.Digit9: return 57;
// 小键盘数字
//case KeyType.Numpad0: return 96;
//case KeyType.Numpad1: return 97;
//case KeyType.Numpad2: return 98;
//case KeyType.Numpad3: return 99;
//case KeyType.Numpad4: return 100;
//case KeyType.Numpad5: return 101;
//case KeyType.Numpad6: return 102;
//case KeyType.Numpad7: return 103;
//case KeyType.Numpad8: return 104;
//case KeyType.Numpad9: return 105;
//case KeyType.NumpadMultiply: return 106;
//case KeyType.NumpadPlus: return 107;
//case KeyType.NumpadEnter: return 108;
//case KeyType.NumpadMinus: return 109;
case KeyType.NumpadDot: return 110;
//case KeyType.NumpadDivide: return 111;
// 功能键
case KeyType.F1: return 112;
case KeyType.F2: return 113;
case KeyType.F3: return 114;
case KeyType.F4: return 115;
case KeyType.F5: return 116;
case KeyType.F6: return 117;
case KeyType.F7: return 118;
case KeyType.F8: return 119;
case KeyType.F9: return 120;
case KeyType.F10: return 121;
//case KeyType.F11: return 122;
//case KeyType.F12: return 123;
// 其他常用键
case KeyType.Backspace: return 8;
//case KeyType.Tab: return 9;
//case KeyType.Clear: return 12;
case KeyType.Enter: return 13;
//case KeyType.Shift: return 16;
//case KeyType.Control: return 17;
//case KeyType.Alt: return 18;
//case KeyType.CapsLock: return 20;
//case KeyType.Esc: return 27;
case KeyType.Space: return 32;
//case KeyType.PageUp: return 33;
//case KeyType.PageDown: return 34;
//case KeyType.End: return 35;
//case KeyType.Home: return 36;
case KeyType.ArrowLeft: return 37;
case KeyType.ArrowUp: return 38;
case KeyType.ArrowRight: return 39;
case KeyType.ArrowDown: return 40;
//case KeyType.Insert: return 45;
//case KeyType.Delete: return 46;
//case KeyType.Help: return 47;
//case KeyType.NumLock: return 144;
default: return 0;
}
}
}