Alice Community  

Go Back   Alice Community > Alice 2 > How do I...?

Reply
 
Thread Tools Display Modes
Find out what key was pressed?
Old
Fiyawerx
Guest
 
Status:
Posts: n/a
Default Find out what key was pressed? - 12-16-2007, 01:49 PM

I'm working on a program where I have a character pick a random letter from a list and shout it out, I need to have an event that checks to see what key was pressed by the user to compare it. My problem is the only way I can seem to get what key was pressed from the 'any key' event is when I use the print function. It points to evenx->KeyChar, but I can't seem to set a variable to this (optimum) or even compare it with an if/else right in the event. Can anyone help?
   
Reply With Quote
Checking key presses
Old
Silver Fox
Guest
 
Status:
Posts: n/a
Default Checking key presses - 12-19-2007, 07:32 PM

Would this be a correct understanding of your question?

The character is playing a guessing game: if the user presses "A" through "D" the program checks whether the key pressed is, say, "C" and takes some action if so and some other action if not.

If that's correct, you could define a method (world-level, or whatever is appropriate) like this:

world.check(String response) {
if (response == "C")
// do something
else
// do something else
}


Having done that, create four separate events, one for each possible key press and each calling world.check, like this

when "A" is typed, do world.check("A")
when "B" is typed, do world.check("B")


and so on. Works like a charm.
   
Reply With Quote
Old
mstram
Guest
 
Status:
Posts: n/a
Default 01-19-2008, 12:23 PM

Quote:
Originally Posted by Silver Fox View Post

Having done that, create four separate events, one for each possible key press and each calling world.check, like this

when "A" is typed, do world.check("A")
when "B" is typed, do world.check("B")


and so on. Works like a charm.
If that's the only way to check multiple keys / events ... then um.. that's really ugly

It would be much more elegant if you could do something like :

when any_key is typed, do world.check(system.key.pressed)


and then some kind of switch/case statement (is there such a thing in Alice ?).

Mike
   
Reply With Quote
Old
rich0e0rick
Senior Member
 
rich0e0rick's Avatar
 
Status: Offline
Posts: 141
Join Date: Jan 2008
Location: Sturgeon Falls
Default 01-19-2008, 07:06 PM

sorry guys, but i think Silver fox has the solution to the problem.....
At least, thats the way i would do it.....


im back!!! FINNALY!!! got windows working on my mac again!!!
   
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



Copyright ©2024, Carnegie Mellon University
Alice 2.x © 1999-2012, Alice 3.x © 2008-2012, Carnegie Mellon University. All rights reserved.