Alice Community  

Go Back   Alice Community > Educators > Teaching with Alice

Thread Tools Display Modes
How to add a 'Detect Key' Function
Mr Kidnapper
Senior Member
Status: Offline
Posts: 442
Join Date: Feb 2011
Default How to add a 'Detect Key' Function - 02-23-2011, 10:29 PM

In this post I will go into detail about Key events.. You don't actually know how useful they are until you're forced to use them, haha. For this function you are required to have prior knowledge of If/Else Booleans and average knowledge of how to read a screen. This event is useful for detecting beyond what the 'When a key is typed' event limits you to, such as F1, parentheses, Num Lock, as well as other basic keyboard functions. Alice does not detect the bells and whistles such as F Lock and customizable macro keys that most gaming keyboards and mice have.
Step 1: Create a 'When a key is typed' event and a world-level Number Variable set to whatever you want. I use 0 or 1. The number of the variable should have no conflicts if you're using it for the sole purpose of detecting keys.
Do not use any variable besides a Number Variable—it just doesn't work.
Step 2: Drag the Number Variable, which I named "Key Pressed" to the 'Do Nothing' portion of the event. When the dialog box pops up, make it Set to world.event.code. This is the first real step towards detecting any given key with Alice, taking no shortcuts.
It should say "When {any key} is typed, do {world.Key Pressed set value to code more...}"
You should no longer do anything to this event, it's complete. Right click the Key Pressed variable in the properties tab and click 'Watch This Variable.' This is so that when you Play the world, you can actually see what you're typing.

Now you have to actually make use of the detection.
Step 3: Create another Event, this time, you should have guessed it, When a Variable Changes. Set the variable in this event to Key Pressed.
Because the event (and hopefully all other subsequent events) only runs when Key Pressed is changed, you don't have any method conflicts. Of course, that assumes that the methods all use different keys. If two or more of them use the same key, then you have a problem on your head.
I should note that when doing this Event, you can choose either the Key Pressed variable or the world.event.code expression, however the first event must set the Key Pressed variable to world.event.code. I don't actually know why it doesn't work without that first requirement—it just doesn't.
Step 4: Create a Method. You surely had some objective in reading this far, but I will stick to something I have already done: A weapon change system.
As you can see, you can set the changed variable event to either code or Number Chosen. Also, there is quite a lot of If/Else and Booleans in there. They're in there for a reason. In the image, the numbers 49 and 35 stand for the number 1. 49 is the number 1 on top of WASD, and 35 is the number 1 on the Numpad. I simply made it possible to do both. If you know of a better way to do this that perhaps involves the For All In Order thing, let me know. I haven't much time to devote to this project lately.
TBA: Detect Code Keymap and Multiple Key Combinations

Last edited by Mr Kidnapper; 02-24-2011 at 08:51 PM.
Reply With Quote
David B
Senior Member
David B's Avatar
Status: Offline
Posts: 1,145
Join Date: Dec 2010
Location: N/A
Default 02-24-2011, 04:17 PM

Could you make the photo smaller? I couldn't see what was written.
Reply With Quote

Thread Tools
Display Modes

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.