Checking if any key is pressed

Help spread the word!Share on FacebookShare on TumblrTweet about this on TwitterShare on Google+Share on LinkedInShare on RedditFlattr the author

anyKeyPressed1

Recently I had needed a way to determine whether any key was pressed on the keyboard. This was because any key could potentially be used as an event trigger, and I didn’t want to assume anything about what those keys might be.

Without this check, the other safest way was to constantly execute the relevant methods, which may lead to reduced performance given a sufficient number of events on the map. Then I remembered there was a keyboard script available, which showed how to check the state of every key.

Using this idea, I could simply check if any of the keys were pressed, and set a flag as needed, and then provide that as a convenient script call.

Download

Script: download here

Installation

In the script editor, place this script below Materials and above Main

Usage

To check whether any key is being pressed, use the script call

Input.any_key_pressed?

Which will return true if any keys are being pressed, and false otherwise.

This may not work for input devices other than the keyboard.

Help spread the word!Share on FacebookShare on TumblrTweet about this on TwitterShare on Google+Share on LinkedInShare on RedditFlattr the author

3 Responses

  1. Zephadus says:

    I’ve recently discovered that this snippet conflicts with your Event Trigger Labels script.
    Any idea why that might be? It throws an error: “SystemStackError occurred. Stack level too deep.”

Leave a Reply

Your email address will not be published.

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax