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

Get help using Construct 2

Post » Tue Feb 21, 2017 3:51 pm

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!
ImageImageImageImage
B
30
S
9
G
7
Posts: 262
Reputation: 5,646

Post » Tue Feb 21, 2017 4:08 pm

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:
Code: Select all
"document.addEventListener('keydown', function(e){if(e.altKey)c2_callFunction('on alt pressed',[]);});"
B
94
S
33
G
118
Posts: 5,396
Reputation: 75,853

Post » Tue Feb 21, 2017 4:27 pm

Neat!!

This "browser->execeute javascript" action is something which I have to keep in mind... thanks!
B
16
S
6
Posts: 26
Reputation: 1,221

Post » Tue Feb 21, 2017 4:32 pm

There's an alternate method here:
viewtopic.php?f=147&t=187452&p=1099108#p1099108
ImageImageImage
B
71
S
22
G
259
Posts: 3,794
Reputation: 143,704

Post » Tue Feb 21, 2017 6:14 pm

R0J0hound wrote: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:
Code: Select all
"document.addEventListener('keydown', function(e){if(e.altKey)c2_callFunction('on alt pressed',[]);});"

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"...

Image
(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?

blackhornet wrote:There's an alternate method here:
viewtopic.php?f=147&t=187452&p=1099108#p1099108

I haven't understood it very well, could you explain it in more detail?
ImageImageImageImage
B
30
S
9
G
7
Posts: 262
Reputation: 5,646

Post » Tue Feb 21, 2017 6:22 pm

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"
ImageImageImage
B
71
S
22
G
259
Posts: 3,794
Reputation: 143,704

Post » Tue Feb 21, 2017 7:15 pm

blackhornet wrote: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?
ImageImageImageImage
B
30
S
9
G
7
Posts: 262
Reputation: 5,646

Post » Tue Feb 21, 2017 8:13 pm

Add it to all the others.
ImageImageImage
B
71
S
22
G
259
Posts: 3,794
Reputation: 143,704

Post » Tue Feb 21, 2017 8:49 pm

blackhornet wrote:Add it to all the others.

Oh, as a "System" - "compare two values".
Thank you!
ImageImageImageImage
B
30
S
9
G
7
Posts: 262
Reputation: 5,646


Return to How do I....?

Who is online

Users browsing this forum: dop2000 and 22 guests