Press a key using WIN32 API in Zig

This commit is contained in:
Marek S. Łukasiewicz 2026-02-08 13:20:56 +01:00
parent 8783d4f43e
commit 917c881da8
5 changed files with 103 additions and 0 deletions

10
src/main.zig Normal file
View file

@ -0,0 +1,10 @@
const std = @import("std");
const win_telegraph = @import("win_telegraph");
pub fn main() !void {
std.debug.print("Will press space in 3 seconds\n", .{});
std.Thread.sleep(3 * std.time.ns_per_s);
win_telegraph.setKey(.SPACE, true);
win_telegraph.setKey(.SPACE, false);
}

20
src/root.zig Normal file
View file

@ -0,0 +1,20 @@
const std = @import("std");
const win32 = @import("win32").everything;
pub const Key = win32.VIRTUAL_KEY;
pub fn setKey(key: Key, pressed: bool) void {
var ip: win32.INPUT = .{
.type = .KEYBOARD,
.Anonymous = .{
.ki = .{
.wVk = key, // virtual-key code for the chosen key
.wScan = 0, // hardware scan code for key
.dwFlags = .{ .KEYUP = if (pressed) 0 else 1 },
.time = 0,
.dwExtraInfo = 0,
},
},
};
_ = win32.SendInput(1, @ptrCast(&ip), @sizeOf(win32.INPUT));
}