1
0
mirror of https://github.com/xiaopeng12138/MaiDXR.git synced 2024-12-18 20:05:52 +01:00
MaiDXR/Assets/Scripts/IOs/ButtonToKey.cs

28 lines
979 B
C#
Raw Normal View History

2022-01-05 19:44:30 +01:00
using System;
using System.Runtime.InteropServices;
using UnityEngine;
2022-02-15 00:37:33 +01:00
using WindowsInput.Native;
2022-01-05 19:44:30 +01:00
public class ButtonToKey : MonoBehaviour
{
[DllImport("user32.dll")]
public static extern uint MapVirtualKey(uint uCode, uint uMapType);
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);
2022-02-15 00:37:33 +01:00
public VirtualKeyCode keyToPress;
2022-01-05 19:44:30 +01:00
private int _insideColliderCount = 0;
2022-01-05 20:38:10 +01:00
private void OnTriggerEnter(Collider other)
2022-01-05 19:44:30 +01:00
{
_insideColliderCount += 1;
2022-03-15 19:02:16 +01:00
keybd_event(System.Convert.ToByte(keyToPress), (byte)MapVirtualKey((uint)keyToPress, 0), 0, UIntPtr.Zero);
2022-01-05 19:44:30 +01:00
}
2022-01-05 20:38:10 +01:00
private void OnTriggerExit(Collider other)
2022-01-05 19:44:30 +01:00
{
_insideColliderCount -= 1;
_insideColliderCount = Mathf.Max(0, _insideColliderCount);
2022-03-15 19:02:16 +01:00
if (_insideColliderCount == 0)
keybd_event(System.Convert.ToByte(keyToPress), (byte)MapVirtualKey((uint)keyToPress, 0), 2, UIntPtr.Zero);
2022-01-05 19:44:30 +01:00
}
}