mirror of
https://github.com/xiaopeng12138/MaiDXR.git
synced 2024-12-18 11:55:52 +01:00
28 lines
979 B
C#
28 lines
979 B
C#
using System;
|
|
using System.Runtime.InteropServices;
|
|
using UnityEngine;
|
|
using WindowsInput.Native;
|
|
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);
|
|
public VirtualKeyCode keyToPress;
|
|
|
|
private int _insideColliderCount = 0;
|
|
private void OnTriggerEnter(Collider other)
|
|
{
|
|
_insideColliderCount += 1;
|
|
keybd_event(System.Convert.ToByte(keyToPress), (byte)MapVirtualKey((uint)keyToPress, 0), 0, UIntPtr.Zero);
|
|
}
|
|
|
|
private void OnTriggerExit(Collider other)
|
|
{
|
|
_insideColliderCount -= 1;
|
|
_insideColliderCount = Mathf.Max(0, _insideColliderCount);
|
|
if (_insideColliderCount == 0)
|
|
keybd_event(System.Convert.ToByte(keyToPress), (byte)MapVirtualKey((uint)keyToPress, 0), 2, UIntPtr.Zero);
|
|
}
|
|
}
|