Project Zomboid

Project Zomboid

BindAid - Input Handler
 This topic has been pinned, so it's probably important
dhert  [developer] 10 Mar, 2023 @ 2:32pm
BindAid - Modder's Resource
Do you or a loved one want to add your own input events, or be sure to not conflict with BindAid? Then look no further!

BindAid does require that it be supported by modders. I cannot create a solution that would make this work for everything. As long as nothing too weird is being done, there should already be compatibility. But to take advantage of the new input handling method, support must be added.

The good news is, this can be done easily and in a way that doesn't make BindAid a hard requirement!

Keyboard Compatibility

DO:
Feel free to make changes to Vanilla functions during `OnGameStart` or before, and be sure to forward your changes!
local _ItemBindingHandler_onKeyPressed = ItemBindingHandler.onKeyPressed ItemBindingHandler.onKeyPressed = function(key) _ItemBindingHandler_onKeyPressed(key) end

Here, I have replaced the `ItemBindingHandler.onKeyPressed` with my own version of the function, and my change is now forwarded as the new `ItemBindingHandler.onKeyPressed` going forward.

DON'T: Make changes to Vanilla functions locally, and not forward your changes.
local _ItemBindingHandler_onKeyPressed = ItemBindingHandler.onKeyPressed Events.OnKeyPressed.Remove(ItemBindingHandler.onKeyPressed) local myNewCoolFunc = function(key) _ItemBindingHandler_onKeyPressed(key) end Events.OnKeyPressed.Add(myNewCoolFunc)
This also prevents other functions from accessing any of your changes, as they will only get the vanilla version of the function and also would not be used at all. This is a sure-fire way to introduce incompatibilities!

The Key Event cache is built at the END of `OnGameStart`, so make any changes that you need before then!

That's basically it as far as compatibility: just make sure you forward your changes made to vanilla functions and be sure to make your changes when it makes sense to, and you're all set!

Mod Load Order

There seems to be some confusion on this topic, likely due to how other games handle mods. But to be clear: Mod Load Order will never change the order that Lua files are loaded. If there is a code conflict, changing the Mod Load Order will not resolve this.

How to use

Support for this mod can be added with just a few additional lines of code! First: get the module, if available. These will be referenced later.

For Keyboard Input:
local keyman = (getActivatedMods():contains("BindAid") and require("KeybindManager"))

For Mouse Input:
local mouse = (getActivatedMods():contains("BindAid") and require("Mouse"))
Last edited by dhert; 10 Mar, 2023 @ 2:48pm
< >
Showing 1-4 of 4 comments
dhert  [developer] 10 Mar, 2023 @ 2:43pm 
Adding a new key

You can add a new key to the game with the following function:
keyman.addKeybind("[SECTION]", "Key_Name", 0)

`[SECTION]` can be any of the available sections, or a new one! This will add the key to the appropriate section on the Keybind Options Tab.

At this point, even if you have already added the key another way, you would typically add a function to an Event to handle your key as well. Instead, we're going to add our key this way:
keyman.addKeyEvents("Key_Name", { ["OnKeyStartPressed"] = myDownFunction, ["OnKeyKeepPressed"] = myHoldFunction, ["OnKeyPressed"] = myUpFunction, })

You ONLY need to add the events that you intend to support!

You can do this in all one go, too!
keyman.addKeybind("[SECTION]", "Key_Name", 0, { ["OnKeyStartPressed"] = myDownFunction, ["OnKeyKeepPressed"] = myHoldFunction, ["OnKeyPressed"] = myUpFunction, })

If you are ADDING a new key bind, it MUST be done during or before `OnGameBoot` to ensure that the game receives this!

NOTE: If a user has the Keyboard Optimize disabled, then your keys will be added as normal events. You will need to plan for this in your function, by first checking the key pressed.

A full example:
-- Demonstrates adding Optional support for BindAid local keyman = (getActivatedMods():contains("BindAid") and require("KeybindManager")) local myKeyFunction = function(key) if key == getCore():getKey("My_Key") then getPlayer():Say("Test Pressed!") elseif key == getCore():getKey("My_Key2") then getPlayer():Say("Test2 Pressed!") end end local onGameBoot = function() -- This is just our Header for the keybinds table.insert(keyBinding, { value = "[Testing]" }) if keyman then -- Add our very own key, in full! keyman.addKeybind("[Testing]", "My_Key", 82, { ["OnKeyPressed"] = myKeyFunction }) -- Let's say we add our key another way... table.insert(keyBinding, { value = "My_Key2", key = 83 }) -- Then we add our "Event" this way: keyman.addKeyEvents("My_Key2", { ["OnKeyPressed"] = myKeyFunction }) else -- Add our keys and our events table.insert(keyBinding, { value = "My_Key", key = 82 }) table.insert(keyBinding, { value = "My_Key2", key = 83 }) -- Add our Event Events.OnKeyPressed.Add(myKeyFunction) end end Events.OnGameBoot.Add(onGameBoot)
Last edited by dhert; 10 Mar, 2023 @ 2:49pm
dhert  [developer] 10 Mar, 2023 @ 2:43pm 
Mouse Buttons

Mouse Button Events are still be developed, in that I am trying to make it easier to handle when a Mouse Button is not available.

For now, you have access to the following variable to tell you how many Mouse Buttons are enabled:
mouse.mouseButtons

Mouse Events work similarly to key bind events.

Events:

Down
Hold
Release
OnMouseMiddleDown
OnMouseMiddleHold
OnMouseMiddleUp
OnMouse4Down
OnMouse4Hold
OnMouse4Up
OnMouse5Down
OnMouse5Hold
OnMouse5Up
OnMouse6Down
OnMouse6Hold
OnMouse6Up
OnMouse7Down
OnMouse7Hold
OnMouse7Up
OnMouse8Down
OnMouse8Hold
OnMouse8Up
OnMouse9Down
OnMouse9Hold
OnMouse9Up
OnMouse10Down
OnMouse10Hold
OnMouse10Up
OnMouse11Down
OnMouse11Hold
OnMouse11Up
OnMouse12Down
OnMouse12Hold
OnMouse12Up
The default Left and Right Click actions are defined in Java, and cannot be overwritten easily. It is beyond the scope of this mod to attempt this as well.

An example:
local mouse = (getActivatedMods():contains("BindAid") and require("Mouse")) local myMouseButton = function(mouseX, mouseY) getPlayer():Say("Middle Mouse!") end local onGameBoot = function() if mouse then mouse.Add({ ["OnMouseMiddleUp"] = myMouseButton, }) end end Events.OnGameBoot.Add(onGameBoot)

I also would like to add support for detecting when these Mouse Buttons are clicked on a UI, but it seems the base game only performs these checks on the main Mouse Buttons. So, stay tuned!
Last edited by dhert; 10 Mar, 2023 @ 2:54pm
dhert  [developer] 10 Mar, 2023 @ 2:45pm 
Modkey

The Modifier Key (or: Modkey) allows for different actions to be done depending on if a player is holding this key. The state of this key can be gotten easily:
local mod = keyman.isModkeyDown()

Controller Support

If you would like to include controllers, then use the following:
local mod = keyman.isModBindDown(player)

`player` must be an `IsoPlayer`.

This will return if the player is using the Modkey OR holding the Left Bumper (L1) on a controller. I recommend this version for full support.

Multiplayer

The state of the Modifier is fully synced in Multiplayer and is available in the player's ModData:
player:getModData().ModBind

`player` must be an `IsoPlayer`.

This is also synced in Singleplayer, so you can rely on this method to retrieve the state of the Modifier as well.
Last edited by dhert; 13 Mar, 2023 @ 1:29am
dhert  [developer] 13 Mar, 2023 @ 1:03am 
This discussion is reserved for Modders only.

If you are having any issues with incorporating support for BindAid into your mod, post here and I'll see if I can help you out.

Other posts will be ignored and deleted; please communicate in the appropriate channels.
Last edited by dhert; 13 Mar, 2023 @ 1:17am
< >
Showing 1-4 of 4 comments
Per page: 1530 50