using System;
using System.Runtime.InteropServices;
namespace BSO
{
/// Tasten der Mod (F8-F11): "gerade gedrueckt" erkennen, nur wenn das Spielfenster vorne ist.
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 };
/// Einmal pro Bild vor allen Pressed-Abfragen.
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;
}
}
}