@@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace BSO
|
||||
{
|
||||
/// <summary>Tasten der Mod (F8-F11): "gerade gedrueckt" erkennen, nur wenn das Spielfenster vorne ist.</summary>
|
||||
public static class Input
|
||||
{
|
||||
[DllImport("user32.dll")] static extern short GetAsyncKeyState(int key);
|
||||
[DllImport("user32.dll")] static extern nint GetForegroundWindow();
|
||||
[DllImport("user32.dll")] static extern uint GetWindowThreadProcessId(nint window, out uint process);
|
||||
|
||||
public const int F8 = 0x77, F9 = 0x78, F10 = 0x79, F11 = 0x7A;
|
||||
|
||||
static readonly bool[] down = new bool[256], pressed = new bool[256];
|
||||
static readonly int[] watched = { F8, F9, F10, F11 };
|
||||
|
||||
/// <summary>Einmal pro Bild vor allen Pressed-Abfragen.</summary>
|
||||
public static void Update()
|
||||
{
|
||||
bool focus = GameHasFocus();
|
||||
foreach (int key in watched)
|
||||
{
|
||||
bool now = focus && GetAsyncKeyState(key) < 0;
|
||||
pressed[key] = now && !down[key];
|
||||
down[key] = now;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool Pressed(int key) => pressed[key];
|
||||
|
||||
public static bool GameHasFocus()
|
||||
{
|
||||
GetWindowThreadProcessId(GetForegroundWindow(), out uint pid);
|
||||
return pid == (uint)Environment.ProcessId;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user