#windows sendinput issues

1 messages · Page 1 of 1 (latest)

topaz aurora
#

Calls separate zig module created for sending os level inputs

cpp

pub fn run(self: *LeaveStarterHouse, img: *const Img, bot: *Bot) !void {
try bot.game.input_manager.change(.right);
fn key_down(key: []const Zkey, action: bool) !void {
        if (action) { // when it should be set to key down
            for (key) |k| {
                try Zinput.send_key_down(k);
            }
            return;
        }

        // for key up
        for (key) |k| {
            try Zinput.send_key_up(k);
        }
    }

pub fn change(self: *InputManager, new_state: Input) !void {
        // input didnt change, do nothing
        if (self.input == new_state) return;

        // switching to none state, release all possible keys
        if (new_state == .none) {
            var iterator = self.keymap.iterator();
            while (iterator.next()) |entry| {
                try key_down(entry.value_ptr.*, false);
            }
            return;
        }

        // release current input keys
        if (self.keymap.get(self.input)) |key| {
            try key_down(key, false);
        }

        self.input = new_state;

        // press new input keys
        if (self.keymap.get(new_state)) |key| {
            try key_down(key, true);
        }
    }
#

module code for SendInput

pub fn send_key_down(key: Key) !void {
        var input: WINDOWS.INPUT = undefined;
        input.type = WINDOWS.INPUT_KEYBOARD;
        input.unnamed_0.ki.wVk = @intFromEnum(key);
        // input.unnamed_0.ki.wScan = 0;
        input.unnamed_0.ki.dwFlags = 0; // No flags for key press
        input.unnamed_0.ki.time = 0;
        input.unnamed_0.ki.dwExtraInfo = 0;

        const s = WINDOWS.SendInput(1, @ptrCast(&input), @sizeOf(WINDOWS.INPUT));
        if (s == 0) {
            return error.SendInputFailed;
        }
    }

    pub fn send_key_up(key: Key) !void {
        var input: WINDOWS.INPUT = undefined;
        input.type = WINDOWS.INPUT_KEYBOARD;
        input.unnamed_0.ki.wVk = @intFromEnum(key);
        // input.unnamed_0.ki.wScan = 0;
        input.unnamed_0.ki.time = 0;
        input.unnamed_0.ki.dwExtraInfo = 0;
        input.unnamed_0.ki.dwFlags = WINDOWS.KEYEVENTF_KEYUP;

        if (WINDOWS.SendInput(1, @ptrCast(&input), @sizeOf(WINDOWS.INPUT)) == 0) {
            return error.SendInputFailed;
        }
    }
#

problem is after calling the send_key_up code, sending keys down does not work anymore. only after restarting send_key_down works, and im also not sure if always since im constantly changing the code

#

@potent thicket help me out 👉👈

potent thicket
#

This is a separate zig module correct?

#

and you're compiling with debug?

topaz aurora
#

const Zinput = @import("zig_key_mouse").Input; is

topaz aurora
#

the input_manager is not a separate module, but my debugger shows me it hit it even though it cant get hit
try bot.game.input_manager.change(.right);

potent thicket
#

This is very odd

topaz aurora
#

also I want to debug with vscodium but I cant use the C/C++ extension there since when I try to debug I get an error message that says debugging only works in vscode. I tried lldb, that runs but doesnt hit breakpoints on windows.

so I setup minimal vscode just for debugging...

potent thicket
#

Like a set of files you can use to replicate it

topaz aurora
#

no I would have to create one. are you fine when I send you the whole thing?

#

I guess that wouldnt even really work, let me try to see if I can reproduce this in a smaller scale ...

topaz aurora
#

Key: "P" to toggle it when its running

have to check if this also has the debugging issues I experienced but sending keys again like it should seems to be broken like in my code

topaz aurora
# potent thicket Yea

okay, the example should behave like my code. would be nice if you could take a look at it

#

I didnt include the .vscode folder with the launch.json and tasks.json, they are just:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Debug exe",
      "type": "cppvsdbg",
      "request": "launch",
      "program": "${workspaceFolder}/zig-out/bin/test.exe",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}/zig-out/bin",
      "symbolSearchPath": "${workspaceFolder}/zig-out/bin",
      "environment": [],
      "externalConsole": false,
      "logging": {
        "moduleLoad": false
      },
      "preLaunchTask": "build_zig"
    }
  ]
}
{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "build_zig",
      "type": "shell",
      "command": "zig build",
      "group": "build",
      "options": {
        "cwd": "${workspaceFolder}"
      },
      "problemMatcher": []
    }
  ]
}
potent thicket
#

what version of zig are you using?

topaz aurora
potent thicket
#

ill have to update

#

how do i test it

topaz aurora
# potent thicket how do i test it

with vscode debugging? I didnt provide the files in the zip, you would have to create a .vscode folder with the files and f5 to start debugging. no further setup should be required, and set breakpoint at try bot.game.input_manager.change(.right); should be line 159

#

and pressing key "p" should toggle the SendInput, after the keys were released and toggling it again, the arrow key right will no longer be pressed. I check that with just an online key test website thing

potent thicket
#

ok it is in fact not hitting that breakpoint

potent thicket
topaz aurora
topaz aurora
#

?

potent thicket
#

is it supposed to hold down the key

topaz aurora
#

when first pressed p, it shows it sends the Right arrow key. when pressing p again it releases all possible keys, and when pressing p again it should hold down right but it doesnt do anything

topaz aurora
#

not sure if its logic issue or wrong winapi calling but hard to tell when I cant really debug it ...

potent thicket
#

nvm i fixed it

#

its pressing my numbad 6 key

#

seems to be working but its pressing my numberpad

topaz aurora
topaz aurora
#

but for me it does not work after it should press down the right arrow key again, it only works after starting the program

potent thicket
#

yea

#

i found a bug in your logic

#

the early exit when you check for .none does an early return so it doesnt set the new state to .none

#

so i just defer self.input = new_state;

#

right under the if early exit

topaz aurora
#

oh you mean I dont even set the none state... ufff, beginner mistake

#

yeah that works now, didnt think I overlook something like that ...

#

but still why is debugging so weird for me here and can I get lldb working in vscodium for debugging?

#

or any kind of debugging in vscodium

potent thicket
#

apparently this might work? it has 5 thumbs up

eager dome
#

for as long as I can remember (or, at least with the self-hosted compiler, I don't remember what stage1 was like anymore) debugging has always done this janky thing where when it's skipping a block the cursor will jump to the last line of that block instead of the brace
I don't know a way around it other than adding a bogus log statement or whatever above the last line in the block, and I've yet to get lldb working with zig on windows
I get the same behavior on remedybg and raddebugger so I suspect it has something to do with how zig produce the pdbs

#

(but don't quote me on that, I don't work on compilers :P)