feat: detect key in background

This commit is contained in:
Gerard Gascón 2024-04-12 15:40:45 +02:00
parent 3e34a7a048
commit 680481afad
13 changed files with 460 additions and 19 deletions

View file

@ -0,0 +1,28 @@
using SatorImaging.AppWindowUtility;
using TMPro;
using UnityEngine;
public class ClickTest : MonoBehaviour {
[SerializeField] private TMP_Text text;
private int _presses;
private bool _pressed;
private const int Key = 0xBF;
private void OnEnable() {
AppWindowUtility.AlwaysOnTop = true;
}
private void Update() {
if (_pressed && Win32API.GetAsyncKeyState(Key) == 0) {
Debug.Log("unpressed");
_pressed = false;
return;
}
if (!_pressed && Win32API.GetAsyncKeyState(Key) != 0) {
Debug.Log("pressed");
_presses++;
text.text = _presses.ToString();
_pressed = true;
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 7a6ab8726a3100840b7f1ba17e278262
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,6 @@
using System.Runtime.InteropServices;
public static class Win32API {
[DllImport("User32.dll")]
public static extern short GetAsyncKeyState(int vKey);
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 8146fbf025e0444dae51ba3f7c693f8a
timeCreated: 1712920866