1
0
mirror of https://github.com/xiaopeng12138/MaiDXR.git synced 2024-11-28 04:50:48 +01:00
MaiDXR/Assets/Scripts/ButtonToKey.cs

39 lines
1.1 KiB
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;
private void Update()
{
if (_insideColliderCount == 0)
{
keybd_event(System.Convert.ToByte(keyToPress), (byte)MapVirtualKey((uint)keyToPress, 0), 2, UIntPtr.Zero);
}
else
{
keybd_event(System.Convert.ToByte(keyToPress), (byte)MapVirtualKey((uint)keyToPress, 0), 0, UIntPtr.Zero);
}
}
2022-01-05 20:38:10 +01:00
private void OnTriggerEnter(Collider other)
2022-01-05 19:44:30 +01:00
{
_insideColliderCount += 1;
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-01-05 19:44:30 +01:00
}
}