Skip to content

Instantly share code, notes, and snippets.

@jesseflorig
Created August 22, 2018 04:17
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jesseflorig/4b9fd832c3db8ee25a52994ec2f77cb4 to your computer and use it in GitHub Desktop.
Save jesseflorig/4b9fd832c3db8ee25a52994ec2f77cb4 to your computer and use it in GitHub Desktop.
Detect key press combos for further evaluation
extends KinematicBody2D
const COMBO_TIMEOUT = 0.3 # Timeout between key presses
const MAX_COMBO_CHAIN = 2 # Maximum key presses in a combo
var last_key_delta = 0 # Time since last keypress
var key_combo = [] # Current combo
func _input(event):
if event is InputEventKey and event.pressed and !event.echo: # If distinct key press down
print(last_key_delta)
if last_key_delta > COMBO_TIMEOUT: # Reset combo if stale
key_combo = []
key_combo.append(event.scancode) # Otherwise add it to combo
if key_combo.size() > MAX_COMBO_CHAIN: # Prune if necessary
key_combo.pop_front()
print(key_combo) # Log the combo (could pass to combo evaluator)
last_key_delta = 0 # Reset keypress timer
func _physics_process(delta):
last_key_delta += delta # Track time between keypresses
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment