Overview
IKeyState is a wrapper around the game keystate buffer, which contains the pressed state for all keyboard keys, indexed by virtual key code.
Namespace
Indexers
Key Access by Virtual Key Code
public bool this[int vkCode] { get; set; }
public bool this[VirtualKey vkCode] { get; set; }
Gets or sets the key-pressed state for a given virtual key code.
The virtual key code to check
True if the specified key is currently pressed
Methods
GetRawValue
public int GetRawValue(int vkCode);
public int GetRawValue(VirtualKey vkCode);
Gets the raw value stored in the index array.
The raw value stored in the index array
SetRawValue
public void SetRawValue(int vkCode, int value);
public void SetRawValue(VirtualKey vkCode, int value);
Sets the raw value in the index array.
The virtual key to change
The raw value to set in the index array
IsVirtualKeyValid
public bool IsVirtualKeyValid(int vkCode);
public bool IsVirtualKeyValid(VirtualKey vkCode);
Gets a value indicating whether the given VirtualKey code is regarded as valid input by the game.
True if the code is valid
GetValidVirtualKeys
public IEnumerable<VirtualKey> GetValidVirtualKeys();
Gets an array of virtual keys the game considers valid input.
An enumerable of valid virtual keys
ClearAll
Clears the pressed state for all keys.
Example Usage
public class MyPlugin : IDalamudPlugin
{
private readonly IKeyState keyState;
private readonly IFramework framework;
public MyPlugin(
IKeyState keyState,
IFramework framework)
{
this.keyState = keyState;
this.framework = framework;
this.framework.Update += OnFrameworkUpdate;
}
private void OnFrameworkUpdate(IFramework framework)
{
// Check if Ctrl key is pressed
if (this.keyState[VirtualKey.CONTROL])
{
Log.Information("Ctrl is pressed");
}
// Check for specific key combination
if (this.keyState[VirtualKey.CONTROL] && this.keyState[VirtualKey.SHIFT])
{
if (this.keyState[VirtualKey.A])
{
Log.Information("Ctrl+Shift+A pressed");
}
}
}
public void CheckKeyState()
{
// Check individual keys
var isWPressed = this.keyState[VirtualKey.W];
var isSpacePressed = this.keyState[VirtualKey.SPACE];
Log.Information($"W: {isWPressed}, Space: {isSpacePressed}");
}
public void Dispose()
{
this.framework.Update -= OnFrameworkUpdate;
}
}
Advanced Usage
Detecting Key Presses
private bool wasKeyPressed = false;
private void OnFrameworkUpdate(IFramework framework)
{
var isKeyPressed = this.keyState[VirtualKey.F1];
// Detect key down (transition from not pressed to pressed)
if (isKeyPressed && !this.wasKeyPressed)
{
Log.Information("F1 was just pressed");
OnF1Pressed();
}
this.wasKeyPressed = isKeyPressed;
}
Multiple Key Combinations
private void CheckHotkeys()
{
// Ctrl+Shift+F
if (this.keyState[VirtualKey.CONTROL] &&
this.keyState[VirtualKey.SHIFT] &&
this.keyState[VirtualKey.F])
{
ToggleWindow();
}
// Alt+Q
if (this.keyState[VirtualKey.MENU] && this.keyState[VirtualKey.Q])
{
QuickAction();
}
}
Checking Valid Keys
public void ListValidKeys()
{
var validKeys = this.keyState.GetValidVirtualKeys();
foreach (var key in validKeys)
{
Log.Information($"Valid key: {key}");
}
}
public void CheckKeyValidity(VirtualKey key)
{
if (this.keyState.IsVirtualKeyValid(key))
{
Log.Information($"{key} is valid for game input");
}
else
{
Log.Warning($"{key} is not valid for game input");
}
}
Clearing Key States
public void ResetAllKeys()
{
// Clear all key states
this.keyState.ClearAll();
Log.Information("All key states cleared");
}
Common Virtual Keys
VirtualKey.CONTROL - Ctrl key
VirtualKey.SHIFT - Shift key
VirtualKey.MENU - Alt key
VirtualKey.ESCAPE - Esc key
VirtualKey.SPACE - Space bar
VirtualKey.RETURN - Enter key
VirtualKey.TAB - Tab key
VirtualKey.F1 through VirtualKey.F12 - Function keys
VirtualKey.A through VirtualKey.Z - Letter keys
VirtualKey.KEY_0 through VirtualKey.KEY_9 - Number keys
VirtualKey.W, VirtualKey.A, VirtualKey.S, VirtualKey.D - WASD keys
- The key state is a combination field with ephemeral states consumed each frame
- Check key state every frame if you need to detect when keys are held down
- For detecting single key presses, track the previous state
- The indexer returns the pressed state (bit 0 of the raw value)
- Raw values include ephemeral states (key down, key up, short press) that are consumed each frame
- Setting key states may be useful for simulating input, but use with caution
- Not all virtual key codes are valid game input - use
IsVirtualKeyValid() to check
- Key state reflects the game’s internal key buffer, not direct OS input