Detect if ANY key is pressed using logic.keyboard.events

I’ve looked through the python reference and while there are functions for each key (i.e. RIGHTSHIFTKEY, ENTERKEY, etc) is there one that is the equivalent of “any” or “all keys”?
The context is that I am trying to create a demo screen with “press any key to continue” purely in python.

You can check if there’s a key pressed in bge.logic.keyboard.active_events. Example blend file attached below.

import bge

def main(cont):
	
	own = cont.owner
	always = cont.sensors["Always"]
	kb = bge.logic.keyboard
	
	if always.positive:
		
		# Checks if there's a key code in active_events dict
		if len(kb.active_events.keys()) > 0:
			
			# Do whatever you want
			own["Text"] = "Key pressed!"

ExAnyKey.blend (92.1 KB)

2 Likes

probably better to use a keypress sensor

keyboard sensor [ANY]-------------python

1 Like

My method.

import bge

def main(cont):
    
#   [ANY KEYBOARD INPUT]
    for key in bge.logic.keyboard.events:
        if bge.logic.keyboard.events[key] == bge.logic.KX_INPUT_JUST_ACTIVATED:
            print("KEYBOARD INPUT")
            
#   [ANY MOUSE INPUT]
    for key in bge.logic.mouse.events:
        if bge.logic.mouse.events[key] == bge.logic.KX_INPUT_JUST_ACTIVATED:
            print("MOUSE INPUT")
1 Like

This is just what I needed. Thank you, RandomPerson. :slight_smile:

1 Like