Skip to main content
Known Participant
August 21, 2007
Question

keyboard event listener

  • August 21, 2007
  • 6 replies
  • 531 views
ok so I want my keyboard event listener to listen for the z key. can you help me without changing my code to mutch? heres my code. I simply want to switch the CONTROL with the letter z.. So how hard is that. because i have been asking around on the internet a while and havent received a good answer.

function detectText2(myevent:KeyboardEvent):void {
if (myevent.keyCode==Keyboard.SPACE) {
flash.media.SoundMixer.stopAll()
}
if (myevent.keyCode==Keyboard.CONTROL) {
var mySound:topsound=new topsound();
mySound.play();
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, detectText2);
This topic has been closed for replies.

6 replies

Known Participant
August 21, 2007
ahh
Craig Grummitt
Inspiring
August 21, 2007
0 refers to the first character in the string "z"
Known Participant
August 21, 2007
ok so it works now thank you very mutch. but the only question I have is why is it a 0 in char code like so charCodeAt(0)) why the 0?
Craig Grummitt
Inspiring
August 21, 2007
yes it makes a difference.

if you are testing the movie(ctrl-Enter), the window that appears has menu options - File, View, Control, Debug.
Inside the Control menu is an option called 'Disable Keyboard Shortcuts'. Once you've disabled keyboard shortcuts the player will receive ALL key-presses(not just some, such as the control key)
Known Participant
August 21, 2007
i see no keyboard shorcuts reference in the control menu. The control menu at the top of the screen to the right of commands and to the left of debug? but that will make a difference anyway? Cause the control key works and plays the sound.
Craig Grummitt
Inspiring
August 21, 2007
you can do it like this:

if (myevent.charCode==("z").charCodeAt(0)) {

or if you need upper or lower case:

if (myevent.charCode==("z").charCodeAt(0) || myevent.charCode==("Z").charCodeAt(0)) {
Known Participant
August 21, 2007
ok so i tried that code you suggested and it didnt work out. no errors but when i pushed the letter z it does not play. i tried putting z's key code in place of the 0 but that did not helo either. I did not mention that I am using CS3 does that make a difference on this topic?
Craig Grummitt
Inspiring
August 21, 2007
are you testing the movie?(ctrl-Enter) if so, make sure that you disable keyboard shortcuts in the Control menu.