Skip to content

How to detect when a key was just pressed (single keypress detection)

💡 Having trouble with Scratch block assembly? Don’t know how to implement code logic? 🚀 Get Help Now

IS

InputHandler_Sam

Posted on July 20, 2025 • Intermediate

⌨️ HELP! Single keypress detection problem!

I’m working on a game and I need to detect when a key was just pressed - not held down! Here’s my situation:

  • I want to detect a single keypress (like jumping or shooting)
  • If the player holds the key down, it should NOT keep triggering
  • I can’t use wait blocks because they pause my entire game loop
  • I need this to work within my main game loop without interruption

Current Problem: When I use “key pressed?” in my forever loop, it keeps triggering while the key is held down. I need it to only trigger ONCE per keypress!

How do I implement proper single keypress detection without breaking my game flow? 🤔

KD

KeyDetection_Expert

Replied 30 minutes later • ⭐ Best Answer

Perfect question @InputHandler_Sam! Single keypress detection is crucial for responsive games. Here are multiple solutions:

🎯 Input Detection System Overview

Here’s how proper keypress detection works:

flowchart TD A[🎮 Game Loop Running] --> B[Check Key State] B --> C{Key Currently Pressed?} C -->|Yes| D{Was Key Pressed Last Frame?} C -->|No| E[Set Key State to Released] D -->|No| F[🎯 KEY JUST PRESSED!] D -->|Yes| G[Key Still Held - Ignore] F --> H[Execute Action] F --> I[Set Key State to Pressed] G --> J[Continue Game Loop] E --> K[Set Key State to Released] H --> L[Continue Game Loop] I --> L J --> L K --> L L --> A style A fill:#e1f5fe style F fill:#e8f5e8 style H fill:#fff3e0 style G fill:#fce4ec

🔧 Method 1: Single Variable Approach (Simplest)

The easiest solution using just one variable:

    // Initialize at start
when flag clicked
set [Space Key State v] to [0]

// Main game loop
forever
// Check for single keypress
if <<key [space v] pressed?> and <(Space Key State) = [0]>> then
// Key was just pressed!
say [Jump!] for (0.5) seconds

// Set state to prevent repeat
set [Space Key State v] to [1]
end

// Reset when key is released
if <not <key [space v] pressed?>> then
set [Space Key State v] to [0]
end

// Rest of your game logic here...
end
  

⚡ Method 2: Multi-Key System

Handle multiple keys efficiently:

    // Initialize all key states
when flag clicked
set [Space Pressed v] to [0]
set [Enter Pressed v] to [0]
set [Up Arrow Pressed v] to [0]

// Main input handler
forever
// Space key detection
if <<key [space v] pressed?> and <(Space Pressed) = [0]>> then
broadcast [space just pressed v]
set [Space Pressed v] to [1]
end
if <not <key [space v] pressed?>> then
set [Space Pressed v] to [0]
end

// Enter key detection
if <<key [enter v] pressed?> and <(Enter Pressed) = [0]>> then
broadcast [enter just pressed v]
set [Enter Pressed v] to [1]
end
if <not <key [enter v] pressed?>> then
set [Enter Pressed v] to [0]
end

// Up arrow detection
if <<key [up arrow v] pressed?> and <(Up Arrow Pressed) = [0]>> then
broadcast [up just pressed v]
set [Up Arrow Pressed v] to [1]
end
if <not <key [up arrow v] pressed?>> then
set [Up Arrow Pressed v] to [0]
end
end
  

🎮 Method 3: Advanced Input Manager

Professional input handling system:

    // Input Manager - runs separately from game loop
when flag clicked
// Initialize input system
set [Input System Active v] to [1]
broadcast [start input manager v]

// Separate input detection script
when I receive [start input manager v]
forever
if <(Input System Active) = [1]> then
// Check all important keys

// Jump key (Space)
if <<key [space v] pressed?> and <not <(Last Space State) = [1]>>> then
broadcast [jump pressed v]
end
set [Last Space State v] to <key [space v] pressed?>

// Attack key (X)
if <<key [x v] pressed?> and <not <(Last X State) = [1]>>> then
broadcast [attack pressed v]
end
set [Last X State v] to <key [x v] pressed?>

// Menu key (Escape)
if <<key [escape v] pressed?> and <not <(Last Escape State) = [1]>>> then
broadcast [menu pressed v]
end
set [Last Escape State v] to <key [escape v] pressed?>
end
end
  

🚀 Method 4: Function-Based Approach

Create reusable input detection:

    // Custom block: Check Single Keypress
define Check Single Keypress (key) (state variable)
if <<key (key) pressed?> and <(state variable) = [0]>> then
set (state variable) to [1]
// Return true (key just pressed)
else
if <not <key (key) pressed?>> then
set (state variable) to [0]
end
// Return false (key not just pressed)
end

// Usage in main loop
when flag clicked
forever
// Check for jump
Check Single Keypress [space v] [Jump State v]
if <(result) = [true]> then
// Handle jump
change y by [50]
end

// Check for shoot
Check Single Keypress [x v] [Shoot State v]
if <(result) = [true]> then
// Handle shooting
broadcast [fire bullet v]
end
end
  

🎯 Method 5: Event-Driven System

Clean separation of input and game logic:

    // Input Event System
when flag clicked
// Input detection (separate from game logic)
forever
// Detect keypresses and broadcast events
if <<key [space v] pressed?> and <(Space State) = [0]>> then
broadcast [on space press v]
set [Space State v] to [1]
end
if <not <key [space v] pressed?>> then
set [Space State v] to [0]
end
end

// Game logic responds to events
when I receive [on space press v]
if <(Game State) = [playing]> then
// Player jump
set [Player Y Velocity v] to [15]
play sound [jump v]
else
if <(Game State) = [menu]> then
// Menu selection
broadcast [menu select v]
end
end
  

🐛 Common Issues & Solutions

  • Key still repeating: Make sure you’re checking the previous state correctly
  • Missed keypresses: Don’t use wait blocks in your input detection loop
  • Laggy response: Keep input detection separate from heavy game logic
  • Multiple triggers: Ensure state variables are properly initialized

💡 Pro Tips

  • Use broadcasts for clean event-driven architecture
  • Keep input detection in a separate forever loop
  • Initialize all state variables when the flag is clicked
  • Consider using custom blocks for reusable input functions

Choose the method that best fits your game’s complexity! 🎮

IS

InputHandler_Sam

Replied 1 hour later

@KeyDetection_Expert This is exactly what I needed! Thank you! 🎉

I implemented Method 1 first and it works perfectly. No more repeated actions when holding keys down. The event-driven system looks really clean too - I’ll try that for my next project!

Quick question: How do I handle key combinations like Ctrl+Space?

KC

KeyCombo_Master

Replied 45 minutes later

@InputHandler_Sam Great follow-up! For key combinations, use this approach:

    // Key combination detection
if <<<key [space v] pressed?> and <key [shift v] pressed?>> and <(Combo State) = [0]>> then
// Super jump activated!
set [Player Y Velocity v] to [25]
set [Combo State v] to [1]
end

// Reset when either key is released
if <<not <key [space v] pressed?>> or <not <key [shift v] pressed?>>> then
set [Combo State v] to [0]
end
  

This handles combinations while maintaining single-press detection! ⌨️

GL

GameLoop_Optimizer

Replied 30 minutes later

Excellent solutions everyone! One performance tip: if you have many keys to check, consider using a list-based approach:

    // Efficient multi-key system
when flag clicked
// Initialize key list
delete all of [Key States v]
add [0] to [Key States v] // Space (index 1)
add [0] to [Key States v] // Enter (index 2)
add [0] to [Key States v] // Up Arrow (index 3)

// Key names list
delete all of [Key Names v]
add [space] to [Key Names v]
add [enter] to [Key Names v]
add [up arrow] to [Key Names v]

// Check all keys efficiently
forever
set [i v] to [1]
repeat (length of [Key Names v])
if <<key (item (i) of [Key Names v]) pressed?> and <(item (i) of [Key States v]) = [0]>> then
broadcast (join [key ] (item (i) of [Key Names v]))
replace item (i) of [Key States v] with [1]
end
if <not <key (item (i) of [Key Names v]) pressed?>> then
replace item (i) of [Key States v] with [0]
end
change [i v] by [1]
end
end
  

This scales well for games with many input keys! 🚀

VB

Vibelf_Community

Pinned Message • Moderator

🚀 Master Advanced Input Systems!

Fantastic discussion on input handling! For those looking to create even more sophisticated control systems, our community can help you implement:

  • 🎮 Custom control mapping and rebinding
  • ⌨️ Complex input sequences and combos
  • 🖱️ Mouse and touch input integration
  • 🎯 Accessibility-friendly input options

📚 Related Discussions

Ready to create professional input systems? Get personalized guidance from our expert tutors in the Vibelf app!