0 Favourites

How do I detect when the "Alt" key is pressed?

  • How can I detect the "Alt" key when it's pressed?

    I tried doing it with the "Keyboard" - "key is down" condition, but it does not let me choose the "Alt" key (not even in the undetected keys drop-down).

    Then I tried with the "Keyboard" - "key code is down", but I haven't found the key code for "Alt" anywhere...

    Do you guys know the key code for it or another way of doing this?

    Thanks!

  • Add the function and browser objects to your project. Add a function and call it "on alt pressed". Next add a "on start of layout" event and add a "browser->execute javascript" action with this:

    "document.addEventListener('keydown', function(e){if(e.altKey)c2_callFunction('on alt pressed',[]);});"[/code:2z17k7e3]
  • Neat!!

    This "browser->execeute javascript" action is something which I have to keep in mind... thanks!

  • There's an alternate method here:

  • Add the function and browser objects to your project. Add a function and call it "on alt pressed". Next add a "on start of layout" event and add a "browser->execute javascript" action with this:

    "document.addEventListener('keydown', function(e){if(e.altKey)c2_callFunction('on alt pressed',[]);});"[/code:3elnakfb]
    

    I'm trying to check when the player presses a key if Alt is pressed and if it isn't, the key's code is added to the variable "Text"...

    (This was the easiest way that I could come up with to register key codes from only certain keys and not from other. Do let me know if there is a simpler way =D)

    Do you want to post the .capx?

    There's an alternate method here:

    I haven't understood it very well, could you explain it in more detail?

  • When Alt is pressed, Keyboard.StringFromKeyCode(Keyboard.LastKeyCode) will actually return the string alt, so you'd need to also check for that. So add something like:

    Keyboard.StringFromKeyCode(Keyboard.LastKeyCode) <> "alt"

  • When Alt is pressed, Keyboard.StringFromKeyCode(Keyboard.LastKeyCode) will actually return the string alt, so you'd need to also check for that. So add something like:

    Keyboard.StringFromKeyCode(Keyboard.LastKeyCode) <> "alt"

    Where should I add it?

  • Add it to all the others.

  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • Add it to all the others.

    Oh, as a "System" - "compare two values".

    Thank you!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)