Hi Glen,
I tried your code (somewhat modified, see below, to get the desired traces)
but it gives me the same result.
E.g. if I hit (right) Alt-5 it traces:
onKeyDown: _ctrlKeyDown=true, _altKeyDown=false, keyCode=17
onKeyDown: _ctrlKeyDown=true, _altKeyDown=true, keyCode=18
onKeyDown: _ctrlKeyDown=true, _altKeyDown=true, keyCode=53
onKeyUp: _ctrlKeyDown=true, _altKeyDown=true, keyCode=53
onKeyUp: _ctrlKeyDown=true, _altKeyDown=false, keyCode=18
Notice that the KEY_DOWN event fires twice for the right alt key!
If I just press and release the right alt key it traces:
onKeyDown: _ctrlKeyDown=true, _altKeyDown=false, keyCode=17
onKeyDown: _ctrlKeyDown=true, _altKeyDown=true, keyCode=18
onKeyUp: _ctrlKeyDown=true, _altKeyDown=false, keyCode=18
So the first trace shows a phantom control key press ...
This lead me to the following work-around (Flash Pro script):
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownOrUpHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyDownOrUpHandler);
var _ctrlKeyDown:Boolean;
var _altKeyDown:Boolean;
function onKeyDownOrUpHandler(e:KeyboardEvent):void
{
if (e.keyCode==17 || e.keyCode==18) // then always test both Ctrl
and Alt to work around possible bug:
{
_ctrlKeyDown = e.keyCode == Keyboard.CONTROL;
_altKeyDown = e.keyCode == Keyboard.ALTERNATE;
}
trace(e.type + ": _ctrlKeyDown=" + _ctrlKeyDown + ", _altKeyDown=" +
_altKeyDown + ", character: " + String.fromCharCode(e.charCode));
}
So the trick is to flag both ctrlKeyIsDown and _altKeyIsDown every time.
This will override the phantom event.
It would be great if you and/or anybody else could confirm the initial bogus
key down event for the right alt key on press.
I tested this so far on two systems with MS Vista and tested with various
debug 10 and 10.1 players, stand alone and in browser. In all cases I got
the bogus control event when pressing alt.
If this is a bug I will log it to Adobe's bug base with the discovered
work-around.
Thanks!
- Benny
By the way this is how I modified your code to get the traces I want (in
Flash Pro):
import flash.events.KeyboardEvent;
const CTRL_KEYCODE:int = 17;
const ALT_KEYCODE:int = 18;
var _ctrlKeyDown:Boolean = false;
var _altKeyDown:Boolean = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUpHandler);
function onKeyDownHandler(e:KeyboardEvent):void
{
switch(e.keyCode) {
case CTRL_KEYCODE:
_ctrlKeyDown = true;
break;
case ALT_KEYCODE:
_altKeyDown = true;
break;
}
trace("onKeyDown: _ctrlKeyDown=" + _ctrlKeyDown + ", _altKeyDown=" +
_altKeyDown + ", keyCode="+e.keyCode);
}
function onKeyUpHandler(e:KeyboardEvent):void
{
switch(e.keyCode) {
case CTRL_KEYCODE:
_ctrlKeyDown = false;
break; //your choice if you put break after return
case ALT_KEYCODE:
_altKeyDown = false;
break;
}
trace("onKeyUp: _ctrlKeyDown=" + _ctrlKeyDown + ", _altKeyDown=" +
_altKeyDown + ", keyCode="+e.keyCode);
}
-----Oorspronkelijk bericht-----
Van: [email protected]
[mailto:[email protected]] Namens Glen Pike
Verzonden: dinsdag 16 november 2010 1:08
Aan: Flash Coders List
Onderwerp: Re: [Flashcoders] How to detect for modifier (alt/ctrl) keys
Yes, but if you don't use keyEvent.ctrlKey in your event handler, but
check for CTRL and set a flag:
e.g.
public static const CTRL_KEYCODE:int = 17;
private var _ctrlKeyDown:Boolean = false;
//etc
function onKeyDownHandler(e:KeyboardEvent):void
{
switch(e.keyCode) {
case CTRL_KEYCODE:
_ctrlKeyDown = true;
return;
break; //your choice if you put break after return
//case ALT_KEYCODE:
// _altKeyDown = true;
// return;
// break;
default:
break;
}
trace("onKeyDown: ctrlKeyDown=" + _ctrlKeyDown);
}
function onKeyUpHandler(e:KeyboardEvent):void
{
switch(e.keyCode) {
case CTRL_KEYCODE:
_ctrlKeyDown = false;
return;
break; //your choice if you put break after return
//case ALT_KEYCODE:
// _altKeyDown = false;
/ return;
// break;
default:
break;
}
trace("onKeyUp: e.ctrlKey=" + e.ctrlKey + ", e.altKey=" + e.altKey);
}
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders