From 917c881da874c36a0683ef6b075c5751048838ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20S=2E=20=C5=81ukasiewicz?= Date: Sun, 8 Feb 2026 13:20:56 +0100 Subject: [PATCH] Press a key using WIN32 API in Zig --- build.zig | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ build.zig.zon | 20 ++++++++++++++++++++ mise.toml | 3 +++ src/main.zig | 10 ++++++++++ src/root.zig | 20 ++++++++++++++++++++ 5 files changed, 103 insertions(+) create mode 100644 build.zig create mode 100644 build.zig.zon create mode 100644 mise.toml create mode 100644 src/main.zig create mode 100644 src/root.zig diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..6e56aaf --- /dev/null +++ b/build.zig @@ -0,0 +1,50 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + + const win32_dep = b.dependency("zigwin32", .{}); + const mod = b.addModule("win_telegraph", .{ + .root_source_file = b.path("src/root.zig"), + .target = target, + .imports = &.{ + .{ .name = "win32", .module = win32_dep.module("win32") }, + }, + }); + + const exe = b.addExecutable(.{ + .name = "win_telegraph", + .root_module = b.createModule(.{ + .root_source_file = b.path("src/main.zig"), + .target = target, + .optimize = optimize, + .imports = &.{ + .{ .name = "win_telegraph", .module = mod }, + }, + }), + }); + b.installArtifact(exe); + + const run_step = b.step("run", "Run the app"); + const run_cmd = b.addRunArtifact(exe); + run_step.dependOn(&run_cmd.step); + run_cmd.step.dependOn(b.getInstallStep()); + if (b.args) |args| { + run_cmd.addArgs(args); + } + + const mod_tests = b.addTest(.{ + .root_module = mod, + }); + const run_mod_tests = b.addRunArtifact(mod_tests); + const exe_tests = b.addTest(.{ + .root_module = exe.root_module, + }); + + const run_exe_tests = b.addRunArtifact(exe_tests); + + const test_step = b.step("test", "Run tests"); + test_step.dependOn(&run_mod_tests.step); + test_step.dependOn(&run_exe_tests.step); +} diff --git a/build.zig.zon b/build.zig.zon new file mode 100644 index 0000000..2c2a65f --- /dev/null +++ b/build.zig.zon @@ -0,0 +1,20 @@ +.{ + .name = .win_telegraph, + .version = "0.0.0", + .fingerprint = 0xd9b97c83665bc0f3, // Changing this has security and trust implications. + .minimum_zig_version = "0.15.2", + .dependencies = .{ + .zigwin32 = .{ + .url = "git+https://github.com/marlersoft/zigwin32.git#3be22a8d33d330662b1fbb5b68c9411bf0293e27", + .hash = "zigwin32-25.0.28-preview-AAAAAB6N5AP3iLHsHKzg7r_aiDyYWo92qLOAG2eBLWhE", + }, + }, + .paths = .{ + "build.zig", + "build.zig.zon", + "paddle", + "src", + "LICENSE", + //"README.md", + }, +} diff --git a/mise.toml b/mise.toml new file mode 100644 index 0000000..0ac46dc --- /dev/null +++ b/mise.toml @@ -0,0 +1,3 @@ +[tools] +zig = "0.15.2" +zls = "0.15.1" diff --git a/src/main.zig b/src/main.zig new file mode 100644 index 0000000..01c8085 --- /dev/null +++ b/src/main.zig @@ -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); +} diff --git a/src/root.zig b/src/root.zig new file mode 100644 index 0000000..5728177 --- /dev/null +++ b/src/root.zig @@ -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)); +}