Introduction
This is the documentation for Ferrum’s developer APIs, as well as some internal functionality. It contains information and recommendations around using Ferrum as a developer. It can also apply as documentation for other similar products with compatible APIs.
Ferrum consists of two APIs: the Legacy API, and the Software API. The Software API is powered by the Ferrum App/Software on the user’s input pc. Currently, Linux users are limited to only using the Legacy API, as the Software API is only compatible with Microsoft Windows.
Key Terms
This section defines some frequently used key terms.
TODO: Attach image of Ferrum board with labelled ports.
Output PC- The PC connected to the Output USB-C port on Ferrum. Commonly referred to as the Main PC or Game PC.Input PC- The PC connected to the Input USB-C port on Ferrum. Commonly referred to as the Second PC or 2pc.Software Provider- The provider of the software that interfaces Ferrum’s APIs to control the peripherals.
Using a Serial Port
In this book, examples are given in terms of serial console input and output. Each line in the example implicitly has a Line Terminator proceeding it, except for the Input Prompt (“>>> “) line, as defined in Command Echoing.
See Command Syntax for how commands are parsed, including sending multiple commands on one line, and how invalid or unsupported commands are handled.
TODO: This section should explain how to connect to a serial port, write to it, and read from it.
Line Terminator
Valid line terminators include any combination of the characters \r and \n. We recommend using either \n or
\r\n, as they are standardized in other specifications.
Command Echoing
For maximum compatibility with software expecting a Python interface, the Ferrum serial interface implements command echoing, as well as an input prompt.
This does not apply to the Legacy API. No command echoing occurs when directly connecting with the Ferrum board.
This means that when a command is sent to Ferrum, it will repeat the command back, followed by the \r\n
line terminator, then send the result of the command, and then send the input prompt (>>> ).
Note that the input prompt is exactly three right facing arrows, followed by a single space, and no line terminator.
Examples
Note these examples use raw data, meaning \r is not a backslash and an r, but instead the carriage return character.
If you don’t understand what this means, try asking ChatGPT, or Googling it.
Using \r\n
Input:
km.left()\r\n
Output:
km.left()\r\n
1\r\n
>>> # Note this line is `>>> ` with a single space.
Using \n
Input:
km.left()\n
Output:
km.left()\r\n # Even though \n was sent, \r\n is echoed back
1\r\n
>>> # Note this line is `>>> ` with a single space.
A Command with No Response
Input:
km.left(1)\r
Output:
km.left(1)\r\n
>>> # Note this line is `>>> ` with a single space.
Disabling Echoing
The km.echo command is used to enable and disable command echoing and the input prompt.
Sending the command with no arguments will return whether echoing is enabled (1) or disabled (0).
Sending the command with one argument will either enable (1) echoing, or disable (0) it.
When echoing is disabled, neither the command nor the input prompt is sent back. Only the result of the command
(followed by \r\n) is sent, and commands with no result send nothing at all. This makes responses easier to parse.
Echoing is enabled by default, and is re-enabled whenever the serial port is closed.
Note: callbacks such as the Key State Change Callback always end with the input prompt, even when echoing is disabled.
Disabling Echoing, then Reading a Button
Input:
km.echo(0)
km.left()
Output:
# km.echo(0) takes effect immediately, so nothing is sent back for it.
1 # Only the result of km.left() is sent.
Reading the Echo State
Input:
km.echo()
Output:
km.echo()
1 # Echoing is enabled.
>>>
Command Syntax
Commands sent over the serial port follow Python function call syntax: name(arg1, arg2, ...), followed by a
Line Terminator. This page describes how Ferrum parses that input.
This does not apply to the Legacy API.
Ignored Characters
Before a command is parsed, all whitespace, and every character other than letters, numbers, and +-_.,()[];=, is
removed. This means spacing does not matter, and quotes are optional.
+ and - are kept so they can be used both as number signs (ex: km.move(-5, +5)) and in command names (ex:
km.lock_mx+).
For example, all of the following are treated identically:
km.move(1, 2)
km.move(1,2)
km . move ( 1 , 2 )
As are these:
km.down('a')
km.down("a")
km.down(a)
A side effect of this is that arguments cannot contain any of the removed characters, nor ;, ,, or =, which have
special meaning. See Keys for how this affects key strings.
Keyword Arguments
Some commands, such as km.controller, take keyword arguments
in the form name=value, like Python. Keyword arguments can be given in any order, and any of them can be left out.
Like Python, None is used as a value to mean “no value”. Its meaning depends on the command.
km.controller(a=1, hold=100)
km.controller(hold=100, a=1) # Identical to the above.
km.controller(a=None)
Multiple Commands per Line
Multiple commands can be sent on a single line by separating them with a ;. Every command is run in order, the line
is echoed back once, and only the result of the last command that produced a result is returned.
Input:
km.left(); km.right()
Output:
km.left(); km.right()
0 # The result of km.right(). The result of km.left() is discarded.
>>>
Invalid Commands
A line that is not a valid command, such as one missing its parentheses, having unbalanced parentheses or brackets, or having extra text after the closing parenthesis, is discarded. Keep this in mind if your software waits for a response.
Input:
km.left
Output:
# Nothing is sent back.
Unsupported Commands
Any command beginning with km. or device. that Ferrum does not support is accepted and ignored. It is echoed back
for KM API compatibility, but has no effect otherwise.
Misc
Reconnecting
Closing and reopening the serial port currently only resets the Command Echoing, which is re-enabled whenever the port is closed.
The Hardware Override
On top of the device control logic (which is what apis like the KM API use to control the mouse), there is a hardware override layer, which is designed to allow the user to take control of their mouse in the event that their software provider incorrectly uses the API.
For example, if the software provider sends km.left(1), and mistakenly never sends km.left(0), the user likely
wouldn’t want their mouse to be stuck down forever. This is where the Hardware Override steps in.
The Hardware Override is a simple state machine, defining when the software input is to be ignored in favor of the hardware input. How it behaves can be seen in the image below.
TODO: Update image to match behavior with timed forced releases.
In this image, HW Press/Release means the physical button on the mouse was pressed/released, and SW Press/Release means
the software sent a down/up request (ex: km.left(0) or km.left(1)).
This image may seem daunting at first, but it can make sense by breaking it down into small steps. Such as by starting
at All Released, and seeing where it goes when you press a button, or send a command. It should hopefully quickly
make sense how the override works, and how a user can press or release a button to override a software input.
Software API
The Software API is the more modern, high-performance, and advanced API available on Ferrum. It supports not only KM style commands (over a serial port), but also KMBox Net style commands (over a UDP Socket), DHZBox style commands (over a UDP socket), as well as a variety of custom commands.
It is made up of the following APIs:
- Keyboard Mouse API - Simple commands to control and read the mouse and keyboard, over the serial port.
- Mouse Proxy API - Intercept and modify every frame of mouse input, over the serial port.
- KMBox Net Style API - Compatibility with KMBox Net software, over the UDP socket.
- DHZBox Style API - Compatibility with DHZBox software, over the UDP socket.
The user can also change how some commands behave via API Tweaks.
The following sections provide information and pseudocode examples for the different commands available.
Keyboard and Mouse API
The Keyboard and Mouse API, aka KM API, is a simple set of commands used to control much of Ferrum’s functionality. It allows software to press and release mouse buttons or keyboard keys. Software can also use it to block keys from being sent to the output computer, or to read the state of a physical button or key on a peripheral.
The following sections provide information and pseudocode examples for the different commands available.
Misc
This contains all miscellaneous command definitions.
Version
The km.version() command can be used to retrieve the device version. This is a more universal command used to
determine hardware being used, such as MAKCU vs Ferrum.
When connected to the Ferrum App, and thus using the Keyboard Mouse API, this command returns “kmbox: Ferrum”, formatted as such for compatibility with legacy software.
To see the Ferrum API version number, use the ferrum.version command.
Examples
Fetching the Device Version
Input:
km.version()
Output:
km.version()
kmbox: Ferrum
>>>
Device Presence
The km.has_mouse(), km.has_keyboard(), and km.has_controller() commands are used to check whether a device is
currently connected and designated as a Mouse, Keyboard, or Controller respectively.
Each command returns 1 if such a device is connected, or 0 if not. Any arguments passed are ignored.
This is useful for checking that the user has set up Ferrum correctly before sending commands, as commands sent to a device type that isn’t connected are silently discarded.
Examples
Checking for a Mouse
Input:
km.has_mouse()
Output:
km.has_mouse()
1 # A device designated as a Mouse is connected.
>>>
Checking for a Keyboard
Input:
km.has_keyboard()
Output:
km.has_keyboard()
0 # No device designated as a Keyboard is connected.
>>>
VID/PID
The device.VID() and device.PID() commands return the USB Vendor ID and Product ID of the peripheral connected to
Ferrum. They exist for compatibility with software written for the KMBox B Pro.
The response is formatted as VID=[vid] or PID=[pid], where the value is a 4 digit lowercase hexadecimal number.
The value is taken from the first connected peripheral, which is not necessarily the one designated as the Mouse. If no
peripheral is connected, the default values of 046d and c53f are returned.
Calling either command with arguments does nothing, and returns no response.
Examples
Reading the VID
Input:
device.VID()
Output:
device.VID()
VID=046d
>>>
Reading the PID
Input:
device.PID()
Output:
device.PID()
PID=c53f
>>>
Mouse
This section covers all km commands that affect devices designated as Mice. A device’s type (Mouse or Keyboard) can be set in the Ferrum Utility on the input computer.
Buttons
There are 5 buttons on any mouse being used on Microsoft Windows.
| Button Name | Button Description |
|---|---|
| left | the left button |
| right | the right button |
| middle | the scroll wheel button |
| side1 | the rear side button |
| side2 | the front side button |
Set Button State
The km.[button_name]([state]) command is used to set the state of any of the mouse buttons.
By replacing button_name with the name of any button, as defined in the Buttons section, this command
can be used for all buttons.
By replacing state with either 1 for pressed, or 0 for releasing a press, the command can be used to force a
button down, or release the force press.
Releases last for a uniformly random time period between 125ms and 175ms. After this time period, the button returns to the physical state.
Presses are indefinite, meaning until the user engages the Hardware Override, or a release or click command is sent, the button will remain pressed.
Note: The click command will override any presses or releases in place.
Examples
Pressing the Left Button
Input:
km.left(1) # 1 for pressing the button
Output:
km.left(1)
>>>
Pressing and Releasing the Front Side Button
Input:
km.side2(1) # 1 for pressing the button
# A slight delay of is recommended here so the click appears realistic.
km.side2(0) # 0 for releasing the button
Output:
km.side2(1)
>>> km.side2(0)
>>>
Click
The km.click([button]) command is used to make the mouse press and release a button over some time.
By replacing button with the number representing a button as shown below, this command can be used for all buttons.
Currently this command presses, delays, releases, delays, and then returns to the hardware state.
There is a delay for how long the press and release are held for. The press and release durations are uniformly random values between 75ms and 125ms, and 125ms and 175ms respectively. For fine control over the duration of the delays, I strongly recommend developers use the commands defined in Set Button State.
Clicks can be overridden by the user via the Hardware Override.
If the button is physically pressed when the click starts, it will be force released after the press delay, and then the release will be removed after the release delay, putting it back in the hardware state (pressed if it’s still physically pressed, or released if the user released the button at some point).
| Button Number | Button Description |
|---|---|
| 0 | the left button |
| 1 | the right button |
| 2 | the scroll wheel button |
| 3 | the rear side button |
| 4 | the front side button |
Examples
Clicking the Left Button
Input:
km.click(0) # 0 for Left
Output:
km.click(0)
>>>
Clicking the Front Side Button
Input:
km.click(4) # 4 for Front Side
Output:
km.click(4)
>>>
Lock
The km.lock_[button_lock_name] command is used to read and write the state of the lock on any mouse button.
By replacing button_lock_name with one of the button lock names defined below, it can be made to work with any button.
Calling the command with no arguments (by using ()) will return the state of the lock on the button.
Calling the command with one argument will set the lock’s state. If the argument is 1 it will enable it. If it is 0,
it will disable it.
When the lock is enabled, any physical button presses will not be sent to the Output PC. The Input PC can still read the button states via either the Get Button State or Button State Event commands. The Input PC can also still press/release the button via either the Click or Press/Release commands.
This is also known as “input masking”, as the physical input is “masked” from the Output PC.
| Button Lock Name | Button Description |
|---|---|
| ml | the left button |
| mr | the right button |
| mm | the scroll wheel button |
| ms1 | the rear side button |
| ms2 | the front side button |
Examples
Locking the Left Mouse Button
Input:
km.lock_ml(1) # Since the value is 1, the lock is now on.
Output:
km.lock_ml(1)
>>>
Locking, Reading, Unlocking, and Reading the Front Side Mouse Button
Input:
km.lock_ms2(1) # The side button will no longer function on the Output PC.
km.lock_ms2()
km.lock_ms2(0) # The side button will now work again on the Output PC.
km.lock_ms2()
Output:
km.lock_ms2(1)
>>> km.lock_ms2()
1 # Since the lock is enabled, this outputs 1.
>>> km.lock_ms2(0)
>>> km.lock_ms2()
0 # Since the lock is disabled, this outputs 0.
>>>
Get Button State
The km.[button_name]() command is used to read the state of any of the mouse buttons.
By replacing button_name with the name of any button, as defined in the Buttons section, this command
can be used for all buttons.
Examples
Reading the Left Button’s State
Input:
km.left()
Output:
km.left()
1 # The Left Button in this case was pressed (1).
>>>
Reading the Front Side Button’s State
Input:
km.side2()
Output:
km.side2()
0 # The Front Side Button in this case was released (0).
>>>
Button State Change Callback
Prefix
The syntax for this function was designed by “I hack”, the developer of MAKCU. It is implemented here for compatibility’s sake. However, it is a rather complex function, and may be difficult to grasp at first. If you want something simple instead, its functionality can be substituted by using the Get Button State command.
As an aside, for those that do read and implement usage of this command, be sure your string parsing of the output of
the Callback accounts for the \0 character. This is known as the null character, and is typically used for marking the
end of a string. As such, your parsing may mistakenly cut off early, thinking that it is the end of the string.
Documentation
The km.buttons command is used to enable and disable the Button State Change Callback.
Sending the command with no arguments will return whether the Callback is enabled (1) or disabled (0).
Sending the command with one argument will either enable (1) the Callback, or disable (0) it.
When the callback is enabled, and a button’s state is changed, such as Left being pressed, or the Front Side button being released, the Callback will send a specific line of text on the serial port, representing the state of all keys.
Specifically, the Callback will send km.[button_states]\r\n>>> , where button_states is the raw bitmap of all button
states.
If you’re not familiar with how binary works, now might be a good time to learn.
Essentially we make a number to represent all buttons. This number is made up of 5 bits, where each bit represents a button, with the lowest being Left, and the highest being the Front Side Button.
When no buttons are pressed, the binary number is 0b00000, which is the decimal number 0, and the character '\0'
(Google/ChatGPT “escape sequence syntax” and what those characters mean if you don’t understand).
When left is pressed: binary = 0b00001, decimal = 1, character = '\1'.
When left and the rear side button are pressed: binary = 0b01001, decimal = 9, character = '\9'.
Then this character is sent in button_state’s place. So as a whole if left and rear side are pressed, the character
is '\9', and the string written to the serial port will be "km.\9\r\n>>> ".
Examples
Enabling the Callback
Input:
km.buttons(1) # Since the value is 1, the Callback is now enabled.
Output:
km.buttons(1)
>>>
Enabling, Reading, Disabling, and Reading the Callback State
Input:
km.buttons(1) # The Callback is now enabled.
km.buttons()
km.buttons(0) # The Callback is now disabled.
km.buttons()
Output:
km.buttons(1)
>>> km.buttons()
1 # Since the Callback is enabled, this outputs 1.
>>> km.buttons(0)
>>> km.buttons()
0 # Since the Callback is disabled, this outputs 0.
>>>
User Input with Callback Enabled
This example has the raw output of the serial console. The backslash prior to characters is not literally sent. If you don’t know what the backslash means in this context, use Google/ChatGPT to learn.
There are no implicit Line Terminators here.
The text in the square brackets is explaining context, and not actually sent on the serial port.
There is no Serial Input here, as the User physically pressing/releasing a button is the cause for the Output.
Output:
[User Has All Keys Released]
[User Presses Left]
km.\1\r\n # Binary 0b00001, Left is Pressed
>>>
[User Releases Left]
km.\0\r\n # Binary 0b00000, Nothing is Pressed
>>>
[User Presses Right]
km.\2\r\n # Binary 0b00010, Right is Pressed
>>>
[User Presses Left]
km.\3\r\n # Binary 0b00011, Left & Right are Pressed
>>>
[User Releases Right]
km.\1\r\n # Binary 0b00001, Left is Pressed
>>>
[User Presses Front Side]
km.\17\r\n # Binary 0b10001, Left & Front Side are Pressed
>>>
[User Presses Back Side]
km.\25\r\n # Binary 0b11001, Left & Front & Back Side are Pressed
>>>
[User Releases Left]
km.\24\r\n # Binary 0b11000, Front & Back Side are Pressed
>>>
[User Releases Front Side]
km.\8\r\n # Binary 0b01000, Back Side is Pressed
>>>
[User Releases Back Side]
km.\0\r\n # Binary 0b00000, Nothing is Pressed
>>>
Footnotes
This is a rather complex feature. If it were up to me, I would rewrite it to be more developer friendly. However, for compatibility’s sake, it must remain.
Eventually, when I write the new API, I’m sure this will be done in an easier way.
For now, read slow, re-read, and ask ChatGPT to summarize if you don’t understand, as it should be simple enough for it to understand very well.
Axes
There are 2 main axes used in the Keyboard Mouse API.
| Axis | Axis Directions |
|---|---|
| x | left and right |
| y | up and down |
There is a third axis used separately for the scroll wheel.
Move
The km.move([x_amount], [y_amount]) command is used to move the mouse cursor around the screen.
The integer x_amount is used to represent the number of units (typically pixels) to move right or left. A positive
value means right, and a negative value means left. Similarly, the integer y_amount controls up or down, where
negative is up, and positive is down.
As per Command Syntax, whitespace between the arguments can be any
width. Both arguments must always be included. If you don’t want to move in an axis, simply send 0 in its place, as
shown in the examples below.
Units are generic, instead of being pixels. This is because of the processing the operating system can do. For example, if a user has mouse acceleration on in Windows, or has their cursor sensitivity changed, an input of 10 may result in a movement different than 10 pixels. Users typically have their device pre-configured to properly make 1 unit = 1 pixel, which is done by using default Windows settings, and disabling Mouse Acceleration.
Examples
Moving Down and Right
Input:
km.move(10, 10) # 10 units Right, 10 units Down
Output:
km.move(10, 10)
>>>
Moving Left
Input:
km.move(-5, 0) # 5 units Left
Output:
km.move(-5, 0)
>>>
Moving Up
Input:
km.move(0, -8) # 8 units Up
Output:
km.move(0, -8)
>>>
Scroll
The km.wheel([amount]) command is used to scroll the mouse wheel a number of times up or down.
When the integer amount is positive, the scroll is up. When it is negative, the scroll is down. The number doesn’t
represent pixels, but rather the number of scrolls to perform. One click in a physical scroll wheel is equivalent to one
scroll up or down.
Values outside the range -128 to 127 are clamped to that range.
Security
To prevent sending suspicious output, please do not use values other than -1 or +1 unless you know what you are doing. While I strongly doubt any games actually check this, I have never seen a mouse send any value other than +1 or -1 as the scroll amount, even mice with unlocking scroll wheels. Therefore, sending any larger values than this could be seen as a sign that the input is emulated and not legitimate.
Examples
Scrolling Up
Input:
km.wheel(1) # 1 for up
Output:
km.wheel(1)
>>>
Scrolling Down
Input:
km.wheel(-1) # -1 for down
Output:
km.wheel(-1)
>>>
Lock
The km.lock_[axis_lock_name] command is used to read and write the state of the lock on any axis.
By replacing axis_lock_name with one of the axis lock names defined below, it can be made to work with any axis.
Calling the command with no arguments (by using ()) will return the state of the lock on the axis.
Calling the command with one argument will set the lock’s state. If the argument is 1 it will enable it. If it is 0,
it will disable it.
When the lock is enabled, any physical movement on the axes will not be sent to the Output PC. The Input PC can still send movement to the Output PC by using the Move command, or scroll by using the Scroll command.
This is also known as “input masking”, as the physical input is “masked” from the Output PC.
Each axis can also have just one of its directions locked. See Directional Locks.
| Axis Lock Name | Axis Direction |
|---|---|
| mx | left and right |
| my | up and down |
| mw | scroll wheel |
Directional Locks
The km.lock_[axis_lock_name]+ and km.lock_[axis_lock_name]- commands work the same way as km.lock_[axis_lock_name],
but only lock or unlock one direction of the axis. + refers to the positive direction, and - to the negative
direction, as defined below.
| Axis Lock Name | Positive (+) | Negative (-) |
|---|---|---|
| mx | right | left |
| my | down | up |
| mw | scroll up | scroll down |
When a direction is locked, physical movement in that direction is not sent to the Output PC, while movement in the other direction is sent like normal.
Each direction’s lock is independent of the other:
- Locking both
+and-locks the whole axis, the same askm.lock_[axis_lock_name](1). - Unlocking one direction of a fully locked axis leaves only the other direction locked.
km.lock_[axis_lock_name](0)unlocks both directions.
Calling a directional command with no arguments returns whether that direction is locked. Calling
km.lock_[axis_lock_name]() returns 1 if either direction is locked.
Examples
Locking the Scroll Wheel
Input:
km.lock_mw(1) # Physical scrolling will no longer be sent to the Output PC.
Output:
km.lock_mw(1)
>>>
Locking the X Axis
Input:
km.lock_mx(1) # Since the value is 1, the lock is now on.
Output:
km.lock_mx(1)
>>>
Locking, Reading, Unlocking, and Reading the Y Axis
Input:
km.lock_my(1) # Any up and down input will no longer be sent to the Output PC.
km.lock_my()
km.lock_my(0) # Up and down input will now be sent again like normal.
km.lock_my()
Output:
km.lock_my(1)
>>> km.lock_my()
1 # Since the lock is enabled, this outputs 1.
>>> km.lock_my(0)
>>> km.lock_my()
0 # Since the lock is disabled, this outputs 0.
>>>
Locking Only Upward Movement
Input:
km.lock_my-(1) # Upward movement is no longer sent, while downward movement still is.
Output:
km.lock_my-(1)
>>>
Combining and Removing Directional Locks on the X Axis
Input:
km.lock_mx+(1) # Rightward movement is locked.
km.lock_mx-(1) # Leftward movement is also locked, so the whole axis is now locked.
km.lock_mx()
km.lock_mx+(0) # Rightward movement is unlocked, leaving only leftward movement locked.
km.lock_mx+()
km.lock_mx-()
Output:
km.lock_mx+(1)
>>> km.lock_mx-(1)
>>> km.lock_mx()
1 # Both directions are locked.
>>> km.lock_mx+(0)
>>> km.lock_mx+()
0 # The positive direction is now unlocked.
>>> km.lock_mx-()
1 # The negative direction is still locked.
>>>
Catch XY
The km.catch_xy([duration], [include_sw_input]) command is used to find the amount of x/y mouse input over the last
duration of time.
The parameter duration is a value in milliseconds up to 1000, declaring how far back in time to sum the x/y input.
Values above 1000 are treated as 1000. If duration is not given (km.catch_xy()), it defaults to 1000.
The parameter include_sw_input is an optional boolean (true or false, with any capitalization), declaring whether
to include both software and hardware input (true), or purely hardware input (false, or no parameter).
The command returns the summed input in the following format: (x, y).
Examples
Summing the Last Second of Hardware Input
Input:
km.catch_xy(1000) # 1000ms = 1 second = max duration
Output:
km.catch_xy(1000)
(500, -500) # The mouse moved 500 to the right, and 500 up in the last second.
>>>
Summing the Last 10 MilliSeconds of Software and Hardware Input
Input:
km.catch_xy(10, true) # 10ms, include software input
Output:
km.catch_xy(10, true)
(-3, 10) # The mouse moved 3 left, and 10 up in the last 10ms.
>>>
Axis State Change Callback
Documentation
The km.axes command is used to enable and disable the Axis State Change Callback.
Sending the command with no arguments will return whether the Callback is enabled (1) or disabled (0).
Sending the command with one argument will either enable (1) the Callback, or disable (0) it.
When the callback is enabled, and input is received on an axis, such as the mouse being moved, or the mouse being scrolled, the Callback will send a specific line of text on the serial port, showing the received movement.
Specifically, the Callback will send Axes([x], [y], [scroll])\r\n>>> , where x is the horizontal movement, y is
the vertical movement, and scroll is the amount scrolled.
Positive x is to the right, while negative is to the left. Positive y is down, while negative is up. Positive
scroll is up, while negative is down.
Typically mice will only send units of 1 (1 or -1) when scrolling, as oppsoed to movement, which normally sends
potentially hundreds of units at once.
Examples
Enabling and Disabling the Callback
Input:
km.axes(1) # Since the value is 1, the Callback is now enabled.
Output:
km.axes(1)
>>>
Enabling, Reading, Disabling, and Reading the Callback State
Input:
km.axes(1) # The Callback is now enabled.
km.axes()
km.axes(0) # The Callback is now disabled.
km.axes()
Output:
km.axes(1)
>>> km.axes()
1 # Since the Callback is enabled, this outputs 1.
>>> km.axes(0)
>>> km.axes()
0 # Since the Callback is disabled, this outputs 0.
>>>
User Input with Callback Enabled
The text in the square brackets is explaining context, and not actually sent on the serial port.
There is no Serial Input here, as the User physically pressing/releasing a button is the cause for the Output.
Output:
[User Moves Right]
Axes(7, 0, 0) # 7 units right, 0 units up, 0 scroll
>>>
[User Moves Left]
Axes(-8, 0, 0) # 8 units left, 0 units up, 0 scroll
>>>
[User Moves Right & Down]
Axes(4, 5, 0) # 4 units right, 5 units down, 0 scroll
>>>
[User Scrolls Up]
Axes(0, 0, 1) # scrolls up
>>>
[User Scrolls Down]
Axes(0, 0, -1) # scrolls down
>>>
Keyboard
This section covers all km commands that affect devices designated as Keyboards. A device’s type (Mouse or Keyboard) can be set in the Ferrum Utility on the input computer.
Keys
There are 255 keys on any keyboard being used on Microsoft Windows.
These keys can all be used by their numeric form, while some can also be used by a string form. The table below defines these values, as well as the name of the physical key each one represents.
Key strings are case-insensitive, and may optionally be wrapped in quotes (ex: km.down(space) or
km.down('space')). Keys without a key string can only be used by their key number.
Some punctuation keys, such as =, /, and ;, have no key string, because those characters cannot be sent as
arguments (see Command Syntax). Use their key number instead.
| Key Number | Key String | Key Name |
|---|---|---|
| 4 | a | A |
| 5 | b | B |
| 6 | c | C |
| 7 | d | D |
| 8 | e | E |
| 9 | f | F |
| 10 | g | G |
| 11 | h | H |
| 12 | i | I |
| 13 | j | J |
| 14 | k | K |
| 15 | l | L |
| 16 | m | M |
| 17 | n | N |
| 18 | o | O |
| 19 | p | P |
| 20 | q | Q |
| 21 | r | R |
| 22 | s | S |
| 23 | t | T |
| 24 | u | U |
| 25 | v | V |
| 26 | w | W |
| 27 | x | X |
| 28 | y | Y |
| 29 | z | Z |
| 30 | 1 | 1 and ! |
| 31 | 2 | 2 and @ |
| 32 | 3 | 3 and # |
| 33 | 4 | 4 and $ |
| 34 | 5 | 5 and % |
| 35 | 6 | 6 and ^ |
| 36 | 7 | 7 and & |
| 37 | 8 | 8 and * |
| 38 | 9 | 9 and ( |
| 39 | 0 | 0 and ) |
| 40 | enter | Return (Enter) |
| 41 | esc | Escape |
| 42 | back | Delete (Backspace) |
| 43 | tab | Tab |
| 44 | space | Spacebar |
| 45 | - | - and _ |
| 46 | = and + | |
| 47 | [ and { | |
| 48 | ] and } | |
| 49 | \ and | | |
| 50 | Non-US # and ~ | |
| 51 | ; and : | |
| 52 | ’ and “ | |
| 53 | Grave Accent and Tilde | |
| 54 | , and < | |
| 55 | . | . and > |
| 56 | / and ? | |
| 57 | caps | Caps Lock |
| 58 | f1 | F1 |
| 59 | f2 | F2 |
| 60 | f3 | F3 |
| 61 | f4 | F4 |
| 62 | f5 | F5 |
| 63 | f6 | F6 |
| 64 | f7 | F7 |
| 65 | f8 | F8 |
| 66 | f9 | F9 |
| 67 | f10 | F10 |
| 68 | f11 | F11 |
| 69 | f12 | F12 |
| 70 | print | Print Screen |
| 71 | scroll | Scroll Lock |
| 72 | pause | Pause |
| 73 | insert | Insert |
| 74 | home | Home |
| 75 | pgup | Page Up |
| 76 | del | Delete Forward |
| 77 | end | End |
| 78 | pgdown | Page Down |
| 79 | right | Right Arrow |
| 80 | left | Left Arrow |
| 81 | down | Down Arrow |
| 82 | up | Up Arrow |
| 83 | Keypad Num Lock and Clear | |
| 84 | Keypad / | |
| 85 | Keypad * | |
| 86 | Keypad - | |
| 87 | Keypad + | |
| 88 | Keypad Enter | |
| 89 | Keypad 1 and End | |
| 90 | Keypad 2 and Down Arrow | |
| 91 | Keypad 3 and Page Down | |
| 92 | Keypad 4 and Left Arrow | |
| 93 | Keypad 5 | |
| 94 | Keypad 6 and Right Arrow | |
| 95 | Keypad 7 and Home | |
| 96 | Keypad 8 and Up Arrow | |
| 97 | Keypad 9 and Page Up | |
| 98 | Keypad 0 and Insert | |
| 99 | Keypad . and Delete | |
| 100 | Non-US \ and | | |
| 101 | aplcat | Application |
| 102 | Power | |
| 103 | Keypad = | |
| 104 | f13 | F13 |
| 105 | f14 | F14 |
| 106 | f15 | F15 |
| 107 | f16 | F16 |
| 108 | f17 | F17 |
| 109 | f18 | F18 |
| 110 | f19 | F19 |
| 111 | f20 | F20 |
| 112 | F21 | |
| 113 | F22 | |
| 114 | F23 | |
| 115 | F24 | |
| 116 | Execute | |
| 117 | Help | |
| 118 | Menu | |
| 119 | Select | |
| 120 | Stop | |
| 121 | Again | |
| 122 | Undo | |
| 123 | Cut | |
| 124 | Copy | |
| 125 | Paste | |
| 126 | Find | |
| 127 | Mute | |
| 128 | Volume Up | |
| 129 | Volume Down | |
| 130 | Locking Caps Lock | |
| 131 | Locking Num Lock | |
| 132 | Locking Scroll Lock | |
| 133 | Keypad Comma | |
| 134 | Keypad Equal Sign | |
| 135 | International1 | |
| 136 | International2 | |
| 137 | International3 | |
| 138 | International4 | |
| 139 | International5 | |
| 140 | International6 | |
| 141 | International7 | |
| 142 | International8 | |
| 143 | International9 | |
| 144 | LANG1 | |
| 145 | LANG2 | |
| 146 | LANG3 | |
| 147 | LANG4 | |
| 148 | LANG5 | |
| 149 | LANG6 | |
| 150 | LANG7 | |
| 151 | LANG8 | |
| 152 | LANG9 | |
| 153 | Alternate Erase | |
| 154 | SysReq/Attention | |
| 155 | Cancel | |
| 156 | Clear | |
| 157 | Prior | |
| 158 | Return | |
| 159 | Separator | |
| 160 | Out | |
| 161 | Oper | |
| 162 | Clear/Again | |
| 163 | CrSel/Props | |
| 164 | ExSel | |
| 165–175 | Reserved | |
| 176 | Keypad 00 | |
| 177 | Keypad 000 | |
| 178 | Thousands Separator | |
| 179 | Decimal Separator | |
| 180 | Currency Unit | |
| 181 | Currency Sub-unit | |
| 182 | Keypad ( | |
| 183 | Keypad ) | |
| 184 | Keypad { | |
| 185 | Keypad } | |
| 186 | Keypad Tab | |
| 187 | Keypad Backspace | |
| 188 | Keypad A | |
| 189 | Keypad B | |
| 190 | Keypad C | |
| 191 | Keypad D | |
| 192 | Keypad E | |
| 193 | Keypad F | |
| 194 | Keypad XOR | |
| 195 | Keypad ^ | |
| 196 | Keypad % | |
| 197 | Keypad < | |
| 198 | Keypad > | |
| 199 | Keypad & | |
| 200 | Keypad && | |
| 201 | Keypad | | |
| 202 | Keypad || | |
| 203 | Keypad : | |
| 204 | Keypad # | |
| 205 | Keypad Space | |
| 206 | Keypad @ | |
| 207 | Keypad ! | |
| 208 | Keypad Memory Store | |
| 209 | Keypad Memory Recall | |
| 210 | Keypad Memory Clear | |
| 211 | Keypad Memory Add | |
| 212 | Keypad Memory Subtract | |
| 213 | Keypad Memory Multiply | |
| 214 | Keypad Memory Divide | |
| 215 | Keypad +/- | |
| 216 | Keypad Clear | |
| 217 | Keypad Clear Entry | |
| 218 | Keypad Binary | |
| 219 | Keypad Octal | |
| 220 | Keypad Decimal | |
| 221 | Keypad Hexadecimal | |
| 222–223 | Reserved | |
| 224 | ctr-l | Left Control |
| 225 | shift-l | Left Shift |
| 226 | alt-l | Left Alt |
| 227 | gui-l | Left GUI |
| 228 | ctr-r | Right Control |
| 229 | shift-r | Right Shift |
| 230 | alt-r | Right Alt |
| 231 | gui-r | Right GUI |
| 232–253 | Reserved | |
| 254 | help | Reserved |
| 255 | Reserved |
The key numbers and names come from the Keyboard/Keypad page (section 10) of the USB HID Usage Tables 1.5 document.
https://usb.org/sites/default/files/hut1_5.pdf
Set Key State
The km.down([key]) and km.up([key]) commands are used to set the state of any of the keyboard keys.
By replacing key with the number or key string of any key, as defined in the Keys section, this
command can be used for all keys.
As their names suggest, down makes the key become pressed, while up makes it force release. For compatibility
reasons, these names are slightly confusing, and so be careful to not mistake down for the km.press command, as
km.press actually performs a click.
Releases last for a uniformly random time period between 125ms and 175ms. After this time period, the button returns to the physical state.
Presses are indefinite, meaning until the user engages the Hardware Override, or a release or click command is sent, the button will remain pressed.
Note: The click command will override any presses or releases in place.
Examples
Pressing the A Key
Input:
km.down(4) # 4 for A
Output:
km.down(4)
>>>
Pressing the Space Key by its Key String
Input:
km.down(space) # Equivalent to km.down(44)
Output:
km.down(space)
>>>
Pressing and Releasing the Left Shift Key
Input:
km.down(225) # 225 for Left Shift
# A slight delay of is recommended here so the click appears realistic.
km.up(225)
Output:
km.down(225)
>>> km.up(225)
>>>
Click
The km.press([key]) command is used to make the keyboard press and release a key over some time. Rather unintuitively,
for compatibility’s sake, the command is called km.press, despite being responsible for a full click of a key.
By replacing key with the number or key string of any key, as defined in the Keys section, this
command can be used for all keys.
Currently this command presses, delays, releases, delays, and then returns to the hardware state.
There is a delay for how long the press and release are held for. The press and release durations are uniformly random values between 75ms and 125ms, and 125ms and 175ms respectively. For fine control over the duration of the delays, I strongly recommend developers use the commands defined in Set Key State.
Clicks can be overridden by the user via the Hardware Override.
If the key is physically pressed when the click starts, it will be force released after the press delay, and then the release will be removed after the release delay, putting it back in the hardware state (pressed if it’s still physically pressed, or released if the user released the key at some point).
Examples
Clicking the Z Key
Input:
km.press(29) # 29 for Z
Output:
km.press(29)
>>>
Clicking the Right Control Key
Input:
km.press(228) # 228 for Right Control
Output:
km.press(228)
>>>
Multiple Key Input
The km.down, km.up, and km.press commands have variants respectively called km.multidown, km.multiup, and
km.multipress. These variants act almost identically to the originals, however they allow as many arguments as the
caller desires, where each extra argument is another key to press/release/click.
These variants allow developers to press, release, or click multiple buttons within the same frame. Keep in mind that
currently developers are expected to add their own delay between presses, and so they should expect the multi variants
of these commands to instantly send all inputs within the same frame. This behavior may be changed in the future.
Note that km.multipress uses a different random click duration for each key.
Examples
Pressing Down A, B, and C Keys All at Once
Input:
km.multidown(4, 5, 6) # 4 for A, 5 for B, 6 for C
Output:
km.multidown(4, 5, 6)
>>>
Clicking Left Shift, H, and I at the Same Time
Input:
km.multipress(225, 11, 12) # 225 for Left Shift, 11 for H, 12 for I
Output:
km.multipress(225, 11, 12) # 225 for Left Shift, 11 for H, 12 for I
>>>
Pressing and Releasing 1, 2, and 3 for Exactly One Frame
Input:
km.multidown(30, 31, 32) # 30 for 1, 31 for 2, 32 for 3
km.multiup(30, 31, 32)
Output:
km.multidown(30, 31, 32)
>>> km.multiup(30, 31, 32)
>>>
Lock
The km.mask([key], [state]) command is used to read and write the state of the lock/mask on any keyboard key.
By replacing key with the number or key string of any key, as defined in the Keys section, this
command can be used for all keys.
Calling the command with state set to 0 will disable the lock, while 1 will enable it.
Calling the command with no second argument will return the state of the lock on the key.
When the lock is enabled, any physical key presses will not be sent to the Output PC. The Input PC can still read the key states via the Get Key State command. The Input PC can also still press/release the key via either the Click or Press/Release commands.
This is also known as “input masking”, as the physical input is “masked” from the Output PC.
Examples
Locking the W Key
Input:
km.mask(26, 1) # Since the value is 1, the lock is now on.
Output:
km.mask(26, 1)
>>>
Locking, Reading, Unlocking, and Reading the Space Key
Input:
km.mask(44, 1) # The space bar will no longer function on the Output PC.
km.mask(44)
km.mask(44, 0) # The space bar will now work again on the Output PC.
km.mask(44)
Output:
km.mask(44, 1)
>>> km.mask(44)
1 # Since the lock is enabled, this outputs 1.
>>> km.mask(44, 0)
>>> km.mask(44)
0 # Since the lock is disabled, this outputs 0.
>>>
Get Key State
The km.isdown([key]) command is used to read the state of any of the keyboard keys.
By replacing key with the number or key string of any key, as defined in the Keys section, this
command can be used for all keys.
The command returns 1 if the key is pressed, or 0 if it is released. If no key is given, or the key is not
recognized, 0 is returned.
Examples
Reading the A Key’s State
Input:
km.isdown(4)
Output:
km.isdown(4)
1 # The A Key in this case was pressed (1).
>>>
Reading the Left Control Key’s State
Input:
km.isdown(224)
Output:
km.isdown(224)
0 # The Left Control Key in this case was released (0).
>>>
Clear Locks
The km.init() command is used to clear all keyboard locks/masks. It is called to make it easy to return to a known
state.
Examples
Clearing all Locks
Input:
km.init()
Output:
km.init()
>>>
Key State Change Callback
The km.keys command is used to enable and disable to the Key State Change Callback.
Sending the command with no arguments will return whether the Callback is enabled (1) or disabled (0).
Sending the command with one argument will either enable (1) the Callback, or disable (0) it.
When the callback is enabled, and a keyboard key’s state is changed, such as A being pressed, or Left Shift being released, the Callback will send a specific line of text on the serial port, representing the state of all keys.
Specifically, the Callback will send Keys([keys])\r\n>>> , where keys is the comma delimited list of decimal key
codes, sorted by increasing decimal value. See Keys for information on which key codes represent which
keys.
Examples
Enabling the Callback
Input:
km.keys(1) # Since the value is 1, the Callback is now enabled.
Output:
km.keys(1)
>>>
Enabling, Reading, Disabling, and Reading the Callback State
Input:
km.keys(1) # The Callback is now enabled.
km.keys()
km.keys(0) # The Callback is now disabled.
km.keys()
Output:
km.keys(1)
>>> km.keys()
1 # Since the Callback is enabled, this outputs 1.
>>> km.keys(0)
>>> km.keys()
0 # Since the Callback is disabled, this outputs 0.
>>>
When the User Presses and Releases Keys, and Callback is Enabled
The text in the square brackets is explaining context, and not actually sent on the serial port.
There is no Input here, as the User physically pressing/releasing a key is the cause for the Output.
Note as stated above that when multiple keys are currently pressed, they are put in the list in increasing decimal order, not in the order they were pressed.
Output:
[User Has All Keys Released]
[User Presses A]
Keys(4) # A = 4 is pressed
>>>
[User Releases A]
Keys() # No keys are pressed
>>>
[User Presses K]
Keys(14) # K = 14 is pressed
>>>
[User Presses C]
Keys(6, 14) # C = 6, and K = 14 are pressed
>>>
[User Releases K]
Keys(6) # C = 6 is pressed
>>>
[User Presses Left Shift]
Keys(6, 225) # C = 6, and Left Shift = 225 are pressed
>>>
[User Presses Backspace]
Keys(6, 42, 225) # C = 6, Backspace = 42, and Left Shift = 225 are pressed
>>>
[User Releases C]
Keys(42, 225) # Backspace = 42, and Left Shift = 225 are pressed
>>>
[User Releases Left Shift]
Keys(42) # Backspace = 42 is pressed
>>>
[User Releases Backspace]
Keys() # No keys are pressed
>>>
Controller
This section covers all km commands that affect devices designated as Controllers, such as Xbox and Playstation controllers.
Axes
A controller has 6 axes. Sticks use the range -32768 to 32767, centered at 0, where positive is up or right, and
negative is down or left.
Triggers use the range 0 (released) to 65535 (fully pressed).
| Axis Names | Axis | Range |
|---|---|---|
| lx | left stick horizontal | -32768 to 32767 |
| ly | left stick vertical | -32768 to 32767 |
| rx | right stick horizontal | -32768 to 32767 |
| ry | right stick vertical | -32768 to 32767 |
| lt, l2 | left trigger | 0 to 65535 |
| rt, r2 | right trigger | 0 to 65535 |
Buttons
A controller has up to 17 buttons. Each can be named by its Xbox or Playstation name (case-insensitive).
When reading every button at once, the buttons are combined into a single number, where each button is one bit. The
Bit column shows each button’s bit, so a button is pressed if buttons & (1 << bit) is not 0.
| Bit | Xbox Names | Playstation Names | Button |
|---|---|---|---|
| 0 | up | up | D-pad up |
| 1 | right | right | D-pad right |
| 2 | down | down | D-pad down |
| 3 | left | left | D-pad left |
| 4 | y | triangle | top face button |
| 5 | b | circle | right face button |
| 6 | a | cross | bottom face button |
| 7 | x | square | left face button |
| 8 | ls | l3 | left stick press |
| 9 | rs | r3 | right stick press |
| 10 | lb | l1 | left bumper |
| 11 | rb | r1 | right bumper |
| 12 | view, back | create | left center button |
| 13 | menu, start | options | right center button |
| 14 | share | touchpad | center button |
| 15 | home, guide | ps | home button |
| 16 | mute | mute button |
Triggers are axes rather than buttons (see Axes), due to being analog.
Note that share refers to the Xbox Share button (the center button). The Playstation 4’s Share button is the left
center button, named create.
Controller State
The km.controller(...) command is used to read the controller’s state, and to override any of its axes and buttons.
Reading the State
Calling the command with no arguments (by using ()) will return the controller’s most recent physical state, as
(buttons, lt, rt, lx, ly, rx, ry).
The axes are in the ranges described in Axes, and buttons is every button combined into a
single number, as described in Buttons.
Overriding Axes and Buttons
Calling the command with keyword arguments (name=value) will override the named axes and buttons, using the names
defined in Axes and Buttons.
- An axis’s value is its new position, such as
lx=-32768(left stick fully left) orrt=65535(right trigger fully pressed). - A button’s value is
1to press it, or0to release it. - A value of
Noneends the override, returning the axis or button to its physical state. - Any axis or button not named is left unchanged.
While overridden, the physical input of that axis or button is not sent to the Output PC, and the overridden value is sent instead.
Hold Duration
Each override only lasts for a short duration, after which the axis or button returns to its physical state. The
duration defaults to 20 milliseconds, and can be set from 0 to 255 milliseconds with the hold keyword argument,
such as hold=100. To hold an override longer, send the command again before it ends.
Each axis and button has its own duration, so sending a command only extends the overrides it names.
This means that if the Input PC stops sending commands, such as if its software crashes, the controller returns to its physical state, instead of being stuck.
Invalid Arguments
If any argument is invalid, such as an unknown name, a value out of range, or an argument without a =, the entire
command is ignored, and nothing is overridden.
Examples
Reading the State
Input:
km.controller()
Output:
km.controller()
(0, 0, 0, 0, 0, 0, 0) # No buttons pressed, released triggers, and centered sticks.
>>>
Holding the Right Stick Up
Input:
km.controller(ry=32767) # The right stick is held fully up for 20 milliseconds.
Output:
km.controller(ry=32767)
>>>
Pressing A for 100 Milliseconds
Input:
km.controller(a=1, hold=100) # Also works as km.controller(cross=1, hold=100).
Output:
km.controller(a=1, hold=100)
>>>
Overriding Multiple Axes and Buttons at Once
Input:
km.controller(lx=-16000, rt=65535, rb=1, x=0) # Left stick halfway left, right trigger fully pressed, RB pressed,
# and X held released.
Output:
km.controller(lx=-16000, rt=65535, rb=1, x=0)
>>>
Ending an Override Early
Input:
km.controller(a=1, hold=255)
km.controller(a=None) # A returns to its physical state immediately.
Output:
km.controller(a=1, hold=255)
>>> km.controller(a=None)
>>>
Controller Axes Callback
Documentation
The km.controller_axes command is used to enable and disable the Controller Axes Callback.
Sending the command with no arguments will return whether the Callback is enabled (1) or disabled (0).
Sending the command with one argument will either enable (1) the Callback, or disable (0) it.
When the callback is enabled, and any physical controller axis changes, such as a stick being moved or a trigger being pressed, the Callback will send a specific line of text on the serial port, showing the position of every axis.
Specifically, the Callback will send ControllerAxes([lt], [rt], [lx], [ly], [rx], [ry])\r\n>>> , using the ranges
described in Axes.
Unlike the mouse’s Axis State Change Callback, which uses relative movement, controller axes are absolute positions.
Examples
Enabling, Reading, Disabling, and Reading the Callback State
Input:
km.controller_axes(1) # The Callback is now enabled.
km.controller_axes()
km.controller_axes(0) # The Callback is now disabled.
km.controller_axes()
Output:
km.controller_axes(1)
>>> km.controller_axes()
1 # Since the Callback is enabled, this outputs 1.
>>> km.controller_axes(0)
>>> km.controller_axes()
0 # Since the Callback is disabled, this outputs 0.
>>>
User Input with Callback Enabled
The text in the square brackets is explaining context, and not actually sent on the serial port.
There is no Serial Input here, as the User physically moving the controller’s sticks and triggers is the cause for the Output.
Output:
[User Pushes the Left Stick Fully Right]
ControllerAxes(0, 0, 32767, 0, 0, 0)
>>>
[User Half Presses the Right Trigger]
ControllerAxes(0, 32768, 32767, 0, 0, 0)
>>>
[User Releases Both]
ControllerAxes(0, 0, 0, 0, 0, 0)
>>>
Controller Buttons Callback
Documentation
The km.controller_buttons command is used to enable and disable the Controller Buttons Callback.
Sending the command with no arguments will return whether the Callback is enabled (1) or disabled (0).
Sending the command with one argument will either enable (1) the Callback, or disable (0) it.
When the callback is enabled, and any physical controller button is pressed or released, the Callback will send a specific line of text on the serial port, showing the state of every button.
Specifically, the Callback will send ControllerButtons([buttons])\r\n>>> , where buttons is every button combined
into a single number, as described in Buttons.
Examples
Enabling, Reading, Disabling, and Reading the Callback State
Input:
km.controller_buttons(1) # The Callback is now enabled.
km.controller_buttons()
km.controller_buttons(0) # The Callback is now disabled.
km.controller_buttons()
Output:
km.controller_buttons(1)
>>> km.controller_buttons()
1 # Since the Callback is enabled, this outputs 1.
>>> km.controller_buttons(0)
>>> km.controller_buttons()
0 # Since the Callback is disabled, this outputs 0.
>>>
User Input with Callback Enabled
The text in the square brackets is explaining context, and not actually sent on the serial port.
There is no Serial Input here, as the User physically pressing/releasing a button is the cause for the Output.
Output:
[User Presses A]
ControllerButtons(64) # Bit 6 (A) is set: 1 << 6 = 64.
>>>
[User Presses LB, Still Holding A]
ControllerButtons(1088) # Bits 6 (A) and 10 (LB) are set: 64 + 1024 = 1088.
>>>
[User Releases Both]
ControllerButtons(0)
>>>
Aliases
This file contains all aliases. Sending the alias typically has the same behavior as sending the original command.
| Alias | Original | Details |
|---|---|---|
| m | km.move | Added for Blurred. |
| km.axis | km.axes |
Examples
Using the m Alias
Input:
m(5, 10) # This is equivalent to km.move(5, 10)
Output:
m(5, 10)
>>>
Mouse Proxy API
The Mouse Proxy API allows software to intercept every frame of input from the user’s mouse (showing the full 8KHz),
and modify that input (or leave it as is) before it is sent to the Output PC. In other words, software sits in the
middle of the mouse and the Output PC, acting as a proxy.
The Mouse Proxy API is currently only available over the virtual serial port.
When the Mouse Proxy API is enabled, all other mouse APIs are disabled/overwritten.
Overview
There are four commands in the Mouse Proxy API:
| Command | Description |
|---|---|
mouse_proxy.start([window_size]) | Enables the proxy, buffering window_size microframes of input. |
mouse_proxy.fetch() | Returns the buffered input that hasn’t been modified yet. |
mouse_proxy.modify([frames]) | Sends the given frames in place of the fetched input. |
mouse_proxy.stop() | Disables the proxy, re-enabling all other mouse APIs. |
The general flow is to start the proxy with a window size, then in a loop, fetch the latest input and modify it,
and finally stop the proxy when you are done.
The window size is set in USB microframes, where 8 microframes is 1ms, and must be between 3 and 240. It is how
long your software has to fetch and modify the input. Any input that is not modified within the window is sent to the
Output PC unmodified. So a window size of 80 (10ms) means your software must call fetch and modify at least
every 10ms to modify all of the input.
Examples
Pseudocode
# Request a 10ms = 80 microframe window, so Ferrum will hold 10ms of input that can be modified.
mouse_proxy.start(80)
loop:
# Request the most recent input. Ferrum will send up to 10ms of raw input over the serial port.
# This is the input the user made on their mouse.
# ex: [(1, 2, 0, 0)] represents 1 unit right, 2 units down, no scroll, no buttons pressed.
frames = mouse_proxy.fetch()
# Re-send the input received, but modified. This is what the Output PC will see.
# ex: In this case, mouse movement was inverted.
mouse_proxy.modify([(-1, -2, 0, 0)])
# Fetch and must be called modify at least every 10ms (window size).
# Any slower, and some input will be sent unmodified.
# So sleeps are for 5ms, giving ample headroom to meet the 10ms max.
sleep(5ms)
# Now that the program is closing, we stop the proxy (optional, but recommended).
mouse_proxy.stop()
Python
This example inverts all of the user’s mouse movement for 5 seconds. Remember to change COM17 to the serial port
Ferrum is using.
import serial
import time
import ast
with serial.Serial("COM17", 9600, timeout=1) as ser:
# Disable command echoing, making responses easier to parse.
ser.write("km.echo(0)\n".encode("utf-8"))
ser.write("mouse_proxy.start(80)\n".encode("utf-8"))
ser.read_all() # Flush
start = time.time()
while time.time() - start < 5.00:
ser.write("mouse_proxy.fetch()\n".encode("utf-8"))
window = ser.read_all().decode("utf-8").rstrip("\r\n")
inputs = ast.literal_eval(window)
if len(inputs) == 0:
continue
response_frames = []
for entry in inputs:
x = entry[0]
y = entry[1]
scroll = entry[2]
buttons = entry[3]
response_frames.append(f"({-x},{-y},{scroll},{buttons})")
response = "[" + ",".join(response_frames) + "]"
ser.write(f"mouse_proxy.modify({response})\n".encode("utf-8"))
ser.write("mouse_proxy.stop()\n".encode("utf-8"))
Footnotes
Exposing this API over the serial port isn’t the best developer experience, as it requires string parsing on both ends. It is planned for access to be added to the Net Socket, as well as an SDK that directly interfaces the driver. Feel free to reach out via Credits and Contact if you have any suggestions or requests.
Start
The mouse_proxy.start([window_size]) command enables the Mouse Proxy.
The integer window_size is the number of frames Ferrum will buffer, in units of USB microframes. A USB microframe is
125 microseconds, meaning there are 8 of them every millisecond (8KHz). So a window_size of 80 means Ferrum holds 80
microframes, which is 80 * 125 / 1000 = 10ms of input.
window_size must be between 3 and 240 (0.375~30ms inclusive). If it is missing or outside this range, the command
is ignored.
Calling start while the proxy is already running restarts it with the new window_size, discarding any buffered
input.
The window is the time available to modify the input. Any input that is not fetched and modified within the window will
be sent to the Output PC unmodified. A larger window gives your software more time to respond, at the cost of more
delay between the user moving their mouse, and the Output PC seeing it.
Once the proxy is started, all other mouse APIs are disabled until it is stopped, or the Ferrum acting as a mouse is reconnected.
Examples
Starting the Proxy with a 10ms Window
Input:
mouse_proxy.start(80) # 80 microframes = 10ms
Output:
mouse_proxy.start(80)
>>>
Starting the Proxy with a 2ms Window
Input:
mouse_proxy.start(16) # 16 microframes = 2ms
Output:
mouse_proxy.start(16)
>>>
Fetch
The mouse_proxy.fetch() command returns all buffered input that has not yet been modified, up to the size of the
window set by Start.
For example, with a window_size of 80, if fetch is called it may return 80 frames (10ms). If it is then followed
by a Modify, and fetch is called again 1ms later, it will only return 8 frames (1ms).
Only Modify removes frames from the buffer. Calling fetch twice without a modify in between returns
the same frames again (along with any new ones), and only the most recent fetch can then be modified.
The frames are returned as a list, where each frame is in the format (x, y, scroll, buttons):
x- The number of units moved right (positive) or left (negative), as in Move.y- The number of units moved down (positive) or up (negative), as in Move.scroll- The amount scrolled, as in Scroll.buttons- The bitmap of all button states, as described in Button State Change Callback.
The list is formatted with a space after the opening bracket and before the closing bracket, such as
[ (1, 2, 0, 0) ]. If there is no new input, or the proxy has not been started, an empty list ([ ]) is returned.
Each call to fetch that returns frames should be followed by a call to Modify.
Examples
Fetching Mouse Movement
Input:
mouse_proxy.fetch()
Output:
mouse_proxy.fetch()
[ (1, 2, 0, 0), (0, 1, 0, 0) ] # 2 frames of input. The user moved right and down.
>>>
Fetching a Left Click
Input:
mouse_proxy.fetch()
Output:
mouse_proxy.fetch()
[ (0, 0, 0, 1), (0, 0, 0, 0) ] # Left was pressed (bitmap 0b00001), then released.
>>>
Fetching with No New Input
Input:
mouse_proxy.fetch()
Output:
mouse_proxy.fetch()
[ ] # The user has not used their mouse since the last fetch.
>>>
Modify
The mouse_proxy.modify([frames]) command tells Ferrum what to send to the Output PC in place of the frames that were
returned by Fetch.
Each call to fetch should be followed by exactly one call to modify, passing in the exact same number of frames as
were returned, in the same (x, y, scroll, buttons) format. The values in the frames can be changed however desired.
To send a frame unmodified, simply pass it back as it was received.
If fetch returned an empty list, there is nothing to modify, and modify does not need to be called.
Any fetched input that is not modified within the window set by Start will be sent to the Output PC
unmodified. If some of the fetched frames have already been sent by the time modify is received, those frames are
skipped, and only the remaining frames are modified.
The entire modify is ignored, with no error returned, if:
- The number of frames doesn’t match the number returned by the last
fetch. - All of the fetched frames have already been sent.
- There was no
fetchsince the lastmodify. - The proxy is not started.
Values outside the valid ranges are clamped: x and y to -32768 to 32767, and scroll to -128 to 127.
Only the lowest 5 bits of buttons are used.
Examples
Inverting Mouse Movement
Input:
mouse_proxy.fetch()
mouse_proxy.modify([(-1, -2, 0, 0), (0, -1, 0, 0)])
Output:
mouse_proxy.fetch()
[ (1, 2, 0, 0), (0, 1, 0, 0) ] # The user moved right and down.
>>> mouse_proxy.modify([(-1, -2, 0, 0), (0, -1, 0, 0)]) # The Output PC sees left and up.
>>>
Blocking Left Click
Input:
mouse_proxy.fetch()
mouse_proxy.modify([(3, 0, 0, 0)])
Output:
mouse_proxy.fetch()
[ (3, 0, 0, 1) ] # The user moved right while holding Left (bitmap 0b00001).
>>> mouse_proxy.modify([(3, 0, 0, 0)]) # Movement is kept, but Left is released.
>>>
Passing Input Through Unmodified
Input:
mouse_proxy.fetch()
mouse_proxy.modify([(5, -2, 1, 0)])
Output:
mouse_proxy.fetch()
[ (5, -2, 1, 0) ]
>>> mouse_proxy.modify([(5, -2, 1, 0)]) # The Output PC sees exactly what the user did.
>>>
Stop
The mouse_proxy.stop() command disables the Mouse Proxy, returning the mouse to normal operation, and re-enabling all
other mouse APIs.
While optional, developers are recommended to stop the proxy when their software is closing.
Examples
Stopping the Proxy
Input:
mouse_proxy.stop()
Output:
mouse_proxy.stop()
>>>
Ferrum API
This is a group of miscellaneous commands specific to Ferrum, which use a ferrum prefix instead of the km prefix.
TODO: The APIs could be restructured to have only KM-style backwards compatible commands exist in the km_api set,
while the Ferrum set of commands contains all custom commands, like km.controller, km.axes, etc.
Version Number
The ferrum.version() command can be used to retrieve the ferrum API version. This is more specific than km.version,
which is typically used to decern hardware.
The response is typically a ‘v’, followed by the Ferrum API version in major.minor.patch form.
Examples
Fetching the Ferrum API Version
Input:
ferrum.version()
Output:
ferrum.version()
v2.0.5
>>>
KMBox Net Style API
For future projects, use of the KMBox Net API is STRONGLY discouraged. This API is not very well made, and overall not fun to work with. I suggest using the DHZBox Style API, or the standard Keyboard and Mouse API instead, as they are much more clear, easier to work with, and easier to add features to.
Ferrum’s Software API supports KMBox Net Style commands via the UDP Socket. Simply connect via the details listed in the
Net API tab in the Ferrum App, and send KMBox Net commands as defined in their documentation:
https://github.com/kvmaibox/kmboxnet. Note that not all commands are supported, but most of the essential ones
are.
DHZBox Style API
Ferrum’s Software API supports DHZBox Style commands via the UDP Socket. Simply connect via the details listed in the
Net API tab in the Ferrum App, and send dhzbox commands as defined in their documentation:
https://docs.qq.com/doc/DT0xnTVpRenBhT2pQ. Note that not all commands are supported, but most of the essential ones
are.
API Tweaks
The Ferrum App has some settings, known as API Tweaks, that let the user change how Ferrum responds to software commands. They are controlled by the user, not by software, but developers should be aware of them, as they can change the result of commands.
In the future, these may able to be controlled via software APIs.
Move Multiplier
Every software mouse movement, whether sent via the KM API, the
KMBox Net Style API, or the DHZBox Style API, is multiplied by
the Move Multiplier before being sent to the Output PC.
Fractional remainders are not lost. Instead, they are carried over and added to the next movement. For example, with a
multiplier of 0.5, sending km.move(1, 0) twice results in one movement of 0 (not sent), then one movement of 1.
Any movement that becomes (0, 0) after multiplication is not sent. A multiplier of 1 leaves movement unchanged.
No Button Release
Normally, software releases (such as km.left(0) or km.up(4)) force the button or key to be released for a short
time, before returning it to its physical state. See Set Button State and
Set Key State.
When No Button Release is enabled, software releases instead only remove any software press, immediately returning the button or key to its physical state. If the user is physically holding the button, it stays pressed.
This applies to releases sent via any API. See The Hardware Override for more on how software and physical input interact.
Legacy API
The Legacy API is an extremely basic set of KM-style Mouse commands used to control Ferrum’s functionality from platforms that don’t support the APIs provided by the Ferrum App.
For the latest features avoid using the Legacy API unless absolutely necessary. The only time it should be used is if the user uses Linux for their input pc.
To connect to the Legacy API, please connect directly to the “Silicon Labs CP210x” COM port on your input pc. The starting port speed is 115200. This speed will reset to 115200 every time the device re-powers.
The following sections provide information and pseudocode examples for the different commands available.
Misc
This contains all miscellaneous command definitions.
Version
The km.version() command in the Legacy API strictly returns kmbox: 2.0.0 Aug 31 2020 21:49:51, matching older KMBox
devices. This is for compatibility with legacy software, which may not be updated to work with newer devices.
Examples
Fetching the Device Version
Input:
km.version()
Output:
km.version()
kmbox: 2.0.0 Aug 31 2020 21:49:51
>>>
Baud
The km.baud([baud_rate]) command in the Legacy API lets developers adjust Ferrum’s hardware baud rate directly. This
command is not available in the Software API due to it using a Virtual API, which means
commands are optimized before being sent to the Ferrum hardware, and always communicated at 3,000,000 baud.
Developers are strongly recommended to not exceed 3Mbd (3000000 baud) with the Ferrum hardware. Data transfer may not be
stable at these speeds, leading to command corruption. Furthermore, 3Mbd is more than fast enough for anything, taking
just 0.050ms to send km.move(100, 100)\r\n. The default speed is 115200 baud, as explained in
Legacy API.
This function has no read feature. In other words, km.baud() does nothing, where other commands such as km.left()
would have returned the state of the button. If you’re able to communicate with the device, then the speed is your
current speed.
Examples
Setting the Baud Rate to Max
Input:
km.baud(3000000)
Output: (None)
Setting the Baud Rate to Default
Input:
km.baud(115200)
Output: (None)
Mouse
This section covers all Legacy Mouse commands.
Buttons
There are 5 buttons on any mouse being used on Microsoft Windows.
| Button Name | Button Description |
|---|---|
| left | the left button |
| right | the right button |
| middle | the scroll wheel button |
| side1 | the rear side button |
| side2 | the front side button |
Set Button State
The Legacy API implementation of this command is identical to the Software API implementation, only command echoing is disabled.
Click
The Legacy API implementation of this command is identical to the Software API implementation, only command echoing is disabled.
Lock
The Legacy API implementation of this command is identical to the Software API implementation, only command echoing is disabled.
Get Button State
The Legacy API implementation of this command is identical to the Software API implementation, only command echoing is disabled.
Button State Change Callback
The Legacy API implementation of this command is identical to the Software API implementation, only command echoing is disabled.
Axes
There are 2 main axes used by the Legacy API.
| Axis | Axis Directions |
|---|---|
| x | left and right |
| y | up and down |
There is a third axis used separately for the scroll wheel.
Move
The Legacy API implementation of this command is identical to the Software API implementation, only command echoing is disabled.
Scroll
The Legacy API implementation of this command is identical to the Software API implementation, only command echoing is disabled.
Lock
The Legacy API implementation of this command is identical to the Software API implementation, only command echoing is disabled.
Credits and Contact
This documentation was written by GameGuy.
The Ferrum Discord can typically be found at discord.gg/Ferrum.
You can find me on Discord under the handle gameguythrowaway, or the id <@515708480661749770>.
I am on Telegram at @GameGuyThrowaway.
Feel free to reach out if you see any mistakes, or if you have any questions about Ferrum or its APIs (anti cheat developers welcome).