Windows
DIe nachfolgende Anleitung in C# erläutert das Ansteuern der F- und P-Taste unter Windows.
Voraussetzungen:
- .NET Framework (oder .NET Core/5+)
- Referenz auf System.Runtime.InteropServices und ggf. System.Windows.Forms
1. P/Invoke-Deklarationen
Fügen Sie diese Deklarationen in einen static class NativeMethods:
using System;
using System.Runtime.InteropServices;
internal static class NativeMethods
{
// 1. Für das Abfragen des Tastenzustands
[DllImport("user32.dll")]
public static extern short GetAsyncKeyState(int vKey);
// 2. Für das Simulieren eines Tastendrucks (SendInput)
[DllImport("user32.dll", SetLastError = true)]
public static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);
public const int INPUT_KEYBOARD = 1;
public const ushort KEYEVENTF_KEYDOWN = 0x0000;
public const ushort KEYEVENTF_KEYUP = 0x0002;
[StructLayout(LayoutKind.Sequential)]
public struct INPUT
{
public int type;
public INPUT_UNION U;
}
[StructLayout(LayoutKind.Explicit)]
public struct INPUT_UNION
{
[FieldOffset(0)] public KEYBDINPUT ki;
// Maus- oder Hardware-Input omitted
}
[StructLayout(LayoutKind.Sequential)]
public struct KEYBDINPUT
{
public ushort wVk; // Virtual-Key-Code
public ushort wScan; // Hardware-Scan-Code (optional)
public uint dwFlags; // KEYEVENTF_*
public uint time; // Zeit (0 = System)
public IntPtr dwExtraInfo; // Zusatzinfo
}
}
2. API-Klasse
Erstellen Sie eine Klasse KeySdk mit vier öffentlichen Methoden:
using System.Windows.Forms;
public static class KeySdk
{
// Virtual-Key-Codes für 'F' und 'P'
private const int VK_F = (int)Keys.F; // 0x46
private const int VK_P = (int)Keys.P; // 0x50
/// <summary>
/// Prüft, ob die F-Taste gerade gedrückt ist.
/// </summary>
public static bool IsFPressed()
=> IsKeyPressed(VK_F);
/// <summary>
/// Prüft, ob die P-Taste gerade gedrückt ist.
/// </summary>
public static bool IsPPressed()
=> IsKeyPressed(VK_P);
/// <summary>
/// Generische Abfrage, ob eine Taste gedrückt ist.
/// </summary>
public static bool IsKeyPressed(int vKey)
{
// High‐Bit (0x8000) gesetzt → Taste wird gehalten
return (NativeMethods.GetAsyncKeyState(vKey) & 0x8000) != 0;
}
/// <summary>
/// Löst einen vollständigen Tastendruck (Down + Up) für 'F' aus.
/// </summary>
public static void SimulateFPress()
=> SimulateKeyPress(VK_F);
/// <summary>
/// Löst einen vollständigen Tastendruck (Down + Up) für 'P' aus.
/// </summary>
public static void SimulatePPress()
=> SimulateKeyPress(VK_P);
/// <summary>
/// Generisches Simulieren eines Tastendrucks.
/// </summary>
public static void SimulateKeyPress(int vKey)
{
var inputs = new NativeMethods.INPUT[2];
// Key-Down
inputs[0] = new NativeMethods.INPUT
{
type = NativeMethods.INPUT_KEYBOARD,
U = new NativeMethods.INPUT_UNION
{
ki = new NativeMethods.KEYBDINPUT
{
wVk = (ushort)vKey,
wScan = 0,
dwFlags = NativeMethods.KEYEVENTF_KEYDOWN,
time = 0,
dwExtraInfo = IntPtr.Zero
}
}
};
// Key-Up
inputs[1] = new NativeMethods.INPUT
{
type = NativeMethods.INPUT_KEYBOARD,
U = new NativeMethods.INPUT_UNION
{
ki = new NativeMethods.KEYBDINPUT
{
wVk = (ushort)vKey,
wScan = 0,
dwFlags = NativeMethods.KEYEVENTF_KEYUP,
time = 0,
dwExtraInfo = IntPtr.Zero
}
}
};
NativeMethods.SendInput((uint)inputs.Length, inputs,
Marshal.SizeOf(typeof(NativeMethods.INPUT)));
}
}
3. Verwendung im Code
// 1) Abfragen, ob F gerade gedrückt ist:
if (KeySdk.IsFPressed())
{
Console.WriteLine("F ist down");
}
// 2) Virtueller Tastendruck:
KeySdk.SimulatePPress(); // sendet P-Down und P-Up
Oder in einem Timer-Event (wie im Demo-Code) abfragen:
private void timer1_Tick(object sender, EventArgs e)
{
if (KeySdk.IsFPressed())
button1.BackColor = Color.Red;
else
button1.BackColor = Color.Silver;
if (KeySdk.IsPPressed())
button2.BackColor = Color.Red;
else
button2.BackColor = Color.Silver;
}
Hinweise
- Rechte: Manche Anwendungen blockieren Eingaben von SendInput aus Sicherheitsgründen.
- Timing: Sie können zwischen Down und Up eine kleine Pause per Thread.Sleep(...) einbauen, um längere Key-Holds zu simulieren.