<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.figuramc.org/index.php?action=history&amp;feed=atom&amp;title=Tutorials%2FKeybinds</id>
	<title>Tutorials/Keybinds - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.figuramc.org/index.php?action=history&amp;feed=atom&amp;title=Tutorials%2FKeybinds"/>
	<link rel="alternate" type="text/html" href="https://wiki.figuramc.org/index.php?title=Tutorials/Keybinds&amp;action=history"/>
	<updated>2026-04-14T23:42:12Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.42.1</generator>
	<entry>
		<id>https://wiki.figuramc.org/index.php?title=Tutorials/Keybinds&amp;diff=119&amp;oldid=prev</id>
		<title>Manuel: Automated upload of converted .txt file.</title>
		<link rel="alternate" type="text/html" href="https://wiki.figuramc.org/index.php?title=Tutorials/Keybinds&amp;diff=119&amp;oldid=prev"/>
		<updated>2024-09-26T20:45:09Z</updated>

		<summary type="html">&lt;p&gt;Automated upload of converted .txt file.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Keybind tutorial&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Through Figura’s keybind API you can have the script listen for key presses to make things happen. Common uses are to trigger animations or toggle modelParts on and off.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Keybinds are unsynced information, meaning that without a ping other players cannot know that you pressed a key at all. This guide will be using pings with all the example keybinds.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Note: Keybinds can be used while the player is unloaded (aka you are out of render distance), if the player API is called during this time your script will error.&amp;#039;&amp;#039;&amp;#039; You can protect yourself from these errors by adding this check: &amp;lt;code&amp;gt;if not player:isLoaded() then return end&amp;lt;/code&amp;gt; as the first line of code run &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;inside the ping.&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;example-keybind&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example Keybind ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
First things first, you need to initialize the keybind&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;local exampleKey = keybinds:newKeybind(&amp;quot;Keybind Name&amp;quot;, &amp;quot;key.keyboard.h&amp;quot;)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
At this point, the keybind will show up in the avatar’s keybind list- accessible via the Figura menu- with the name Keybind Name and assigned to the letter H. But pressing H won’t do anything yet.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
More keybinds ids can be found in the [[../enums/Keybinds-List.md|KeybindsList]] page&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
There are multiple ways to detect keybinds, but the most common is through &amp;lt;code&amp;gt;press&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;release&amp;lt;/code&amp;gt; as they are easiest to ping. If you’re not familiar with pings see [[./Pings|Pings]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Underneath creating the key we will be tying the press of the key to a ping function. It’s done underneath as the code is read top-down and the key must exist first.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;local exampleKey = keybinds:newKeybind(&amp;quot;Keybind Name&amp;quot;, &amp;quot;key.keyboard.h&amp;quot;, false)&lt;br /&gt;
&lt;br /&gt;
exampleKey.press = pings.examplePing&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This itself won’t do anything until we create the function pings.examplePing, this must be done above where press is assigned to the ping function, because the ping function will need to exist before it can be assigned. If it’s done beneath nothing will happen.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The false at the end decides whether or not the keybind will function while a gui like the inventory is opening. It can be skipped and the value will be considered false. If it’s set to true then this keybind will run even while any gui is open or closed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;function pings.examplePing()&lt;br /&gt;
&lt;br /&gt;
    log(&amp;quot;Pressed!&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local exampleKey = keybinds:newKeybind(&amp;quot;Keybind Name&amp;quot;, &amp;quot;key.keyboard.h&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
exampleKey.press = pings.examplePing&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And there we have it! Now this keybind will send Pressed! in chat every time H is pressed. At this point you could put whatever lines of code you wish into the ping function and it will be synced.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Alternatively, &amp;lt;code&amp;gt;release&amp;lt;/code&amp;gt; will run the keybind when the key is released rather than when it is first pressed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;toggling-with-a-keybind&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Toggling With A Keybind ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This is going to look extremely similar to the last example, but we are going to add in a boolean that we’re toggling&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;local keybindState = true&lt;br /&gt;
&lt;br /&gt;
-- Here the keybindState is true, meaning the first press will swap it to false&lt;br /&gt;
&lt;br /&gt;
-- If you wish the first press to swap to false, change the true to false above&lt;br /&gt;
&lt;br /&gt;
function pings.examplePing(state)&lt;br /&gt;
&lt;br /&gt;
    log(&amp;quot;state is &amp;quot; .. tostring(state))&lt;br /&gt;
&lt;br /&gt;
    models:setVisible(state)&lt;br /&gt;
&lt;br /&gt;
    -- This will toggle the visibility of all or models, add in a model path to turn on/off specific modelParts&lt;br /&gt;
&lt;br /&gt;
    -- animations.bbmodelName.animationName:setPlaying(not state)&lt;br /&gt;
&lt;br /&gt;
    -- And this is an example of toggling an animation on/off, I&amp;#039;m using not state here because the first press will set this toggle to false and thusly stop the animation, swapping the boolean value like this will make the first press play it&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local exampleKey = keybinds:newKeybind(&amp;quot;Keybind Name&amp;quot;, &amp;quot;key.keyboard.h&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
exampleKey.press = function()&lt;br /&gt;
&lt;br /&gt;
    keybindState = not keybindState&lt;br /&gt;
&lt;br /&gt;
    -- This not is flipping the boolean value between true and false&lt;br /&gt;
&lt;br /&gt;
    pings.examplePing(keybindState)&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- This time .press is being tied to a function that is then calling the ping, instead of being &amp;#039;attached&amp;#039; to it directly.&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;detecting-when-a-key-is-held-down&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Detecting When A Key Is Held Down ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you have the know-how it is possible to use the &amp;lt;code&amp;gt;isPressed()&amp;lt;/code&amp;gt; function to detect when a key is being held down, but it’s not recommended, as using press and release in conjunction is far more effective.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;local keybindState = false&lt;br /&gt;
&lt;br /&gt;
-- keybindState is the variable you will be using to keep track of the pressed-ness of the keybind&lt;br /&gt;
&lt;br /&gt;
function pings.examplePing(state)&lt;br /&gt;
&lt;br /&gt;
    keybindState = state&lt;br /&gt;
&lt;br /&gt;
    -- keybindState is made equivalent to the state sent by press or release for use in other parts of the script&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local exampleKey = keybinds:newKeybind(&amp;quot;Keybind Name&amp;quot;, &amp;quot;key.keyboard.h&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
exampleKey.press = function()&lt;br /&gt;
&lt;br /&gt;
    pings.examplePing(true)&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- Here, examplePing is sending the boolean value true to the ping function&lt;br /&gt;
&lt;br /&gt;
exampleKey.release = function()&lt;br /&gt;
&lt;br /&gt;
    pings.examplePing(false)&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
-- When it&amp;#039;s released, the boolean value false will be sent, indicating that the key is no longer being pressed&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
-- This is unnecessary, but can be used to track the state of keybindState so you can see it working, at this point you can use keybindState wherever and however you wish- as long as it&amp;#039;s in the same script file&lt;br /&gt;
&lt;br /&gt;
function events.tick()&lt;br /&gt;
&lt;br /&gt;
    log(keybindState)&lt;br /&gt;
&lt;br /&gt;
end&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;canceling-a-key-press&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Canceling A Key Press ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A key press can be “cancelled” (Minecraft won’t register it as being pressed) using &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt;. Specifically, if you &amp;lt;code&amp;gt;return true&amp;lt;/code&amp;gt; in the function for the key press it will cancel the press. Returning a non-truthy value will not cancel the key press, so you can use a toggling variable to control the return. Note that returning in the ping will not cancel the key press, only doing it inside the keybind’s press function will cancel it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Below is an example for cancelling the detection of the &amp;lt;code&amp;gt;w&amp;lt;/code&amp;gt; key while also sending a ping.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- prettier-ignore --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;function pings.examplePing()&lt;br /&gt;
&lt;br /&gt;
    log(&amp;quot;Pressed!&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local exampleKey = keybinds:newKeybind(&amp;quot;Keybind Name&amp;quot;, &amp;quot;key.keyboard.w&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
exampleKey.press = function() &lt;br /&gt;
&lt;br /&gt;
    pings.examplePing() return true &lt;br /&gt;
&lt;br /&gt;
end&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you want to cancel without sending a ping, that is completely possible. But the return always has to be at the end of the function.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;using-a-vanilla-keybind&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Using A Vanilla Keybind ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you want to detect a vanilla action like attacking or walking forwards but want it to be compatible in the case that someone bound forward to an arrow key you can directly get the vanilla keybind and use it. There’s multiple ways to accomplish this but we’ll use the same method as previous examples.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;local exampleKey = keybinds:newKeybind(&amp;quot;Keybind Name&amp;quot;, keybinds:getVanillaKey(&amp;quot;key.forward&amp;quot;))&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will now detect the forward key regardless of what it’s bound to. &amp;lt;code&amp;gt;getVanillaKey()&amp;lt;/code&amp;gt; is going to need a key id from a specific list of ids that all correspond to a vanilla keybind. They can be found in the [[../enums/KeyIDs.md|keyIDs]] enum.&lt;/div&gt;</summary>
		<author><name>Manuel</name></author>
	</entry>
</feed>