Last Key Pressed#
Input / Last Key Pressed
Can Read
Cannot Write
Description#
The name of the last Keyboard key that was pressed down
Code#
using System;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;
using VisualScript.Icons;
namespace VisualScript.Runtime
{
[Get]
[Title("Last Key Pressed")]
[Category("Input/Last Key Pressed")]
[Description("The name of the last Keyboard key that was pressed down")]
[Image(typeof(IconKeyPress), ColorTheme.Type.Blue)]
[Keywords("Rebind", "Remap", "Listen", "Record", "Capture")]
[Serializable]
public class TypeStringInputKeyboardLastKey : TypeString
{
[NonSerialized] private Key m_LastKey = Key.None;
[NonSerialized] private int m_LastFrame = -1;
public override string Get(Args args)
{
Keyboard keyboard = Keyboard.current;
if (keyboard == null) return string.Empty;
if (this.m_LastFrame != Time.frameCount)
{
this.m_LastFrame = Time.frameCount;
if (keyboard.anyKey.wasPressedThisFrame)
{
foreach (KeyControl keyControl in keyboard.allKeys)
{
if (!keyControl.wasPressedThisFrame) continue;
this.m_LastKey = keyControl.keyCode;
break;
}
}
}
return this.m_LastKey != Key.None ? this.m_LastKey.ToString() : string.Empty;
}
public override string ToString()
{
return "Last Key Pressed";
}
protected override SubscriptionMode Subscription => SubscriptionMode.OnChange;
}
}