View Full Version : Problem with Key-Handler
MT3
April 9th, 2004, 04:52 AM
Hello!
I have programming a flash projekt for a multimedia pc. Buttons can be controlled by mouse and keyboard.
But on pressing a key on the remote control by IRA/Girder I have problems with the Flash Key-Handler. In fact Flash give me the right key code Key.getCode(), but it doesn't handle onKeyDown()?!
Know anybody about this problem? Maybe there is a bug in Girder or Flash?!
Please help me! Thanks!
MT3
April 9th, 2004, 06:04 AM
I have tested the key handle in javascript! There is the same problem!
Look at this: http://www.marel.at/testKeyHandle.htm
Promixis
April 9th, 2004, 06:29 AM
I am not sure what the problem is... Is it that when Girder sends a keyboard press out that the flash handler doesn't detect it?
MT3
April 9th, 2004, 06:35 AM
I am not sure what the problem is... Is it that when Girder sends a keyboard press out that the flash handler doesn't detect it?
Thats true! But not only Flash, also in JavaScript I couldn't handle pressing keys.
Do you have Girder installed? Please check out this little script: http://www.marel.at/testKeyHandle.htm
If I pressed a key with my remote control (by IRA/Girder) the script output the right key code, but the key handles (onKeyDown, onKeyUp, ..) doesn't working.. --> see Key-Counter on the script!
EDIT: For better understanding: The handles onKeyDown and onKeyUp return true, if a key is pressed down or up. getCode return the last pressing key code (ex. 38 for UP, 40 for DOWN).
Promixis
April 9th, 2004, 06:53 AM
Ok, I tested it out.
By using F5 to send the key, I cannot see a down event. BUT if I hold the F5 key down to rapidly send events, I occasionaly see a down event. I think Girder presses and releases the key very quickly but I am sure it generates the event..
MT3
April 9th, 2004, 07:31 AM
Ok, that's a possible theory ;)
Is there any option to set a value for pressing keys, how long the key should sending?
Flash exist on single frames (default: 12 frames per second) and its very improbable to get this fast key information from girder while one frame is loading..
Thank you for helping!!
MT3
April 13th, 2004, 08:07 AM
I have solved the problem by using javascript!
Here is the code, if anyone is interested:
HTML
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<meta http-equiv=Content-Type content="text/html; charset=ISO-8859-1">
<TITLE>menu</TITLE>
<script type="text/javascript" language="JavaScript">
<!--
function setFlashVar()
{
var oEventTime = new Date(); // js-date object
var iEventTime = oEventTime.getTime(); // get timestamp in milliseconds
// Change flash variables (example for root-layer)
document.getElementById("menu").SetVariable("_root.keyPressedTime", iEventTime);
document.getElementById("menu").SetVariable("_root.keyCode", event.keyCode);
}
//-->
</script>
</HEAD>
<BODY bgcolor="#333333" onkeydown="setFlashVar();" scrolling=0 topmargin=0 leftmargin=0 marginwidth=0 marginheight=0>
<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
onfocus="window.focus();"
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"
WIDTH="550" HEIGHT="400" id="menu" ALIGN="">
<PARAM NAME=movie VALUE="menu.swf">
<PARAM NAME=quality VALUE=high>
<PARAM NAME=bgcolor VALUE=#FFFFFF>
<EMBED
src="menu.swf"
quality=high
bgcolor=#FFFFFF
WIDTH="550"
HEIGHT="400"
NAME="menu"
TYPE="application/x-shockwave-flash"
PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer">
</EMBED>
</OBJECT>
</BODY>
</HTML>
Flash-Movie (ActionScript - example for root layer)
var keyPressedTime = 0;
var keyCode = 255;
this.onEnterFrame = function () {
// Watch the variable keyPressedTime
this.watch("this.keyPressedTime", this.execFunction(this.keyCode));
// If keyPressedTime change their value execute the function on second parameter
}
function execFunction(keyCode) {
this.keyCode = keyCode;
if (this.keyCode==Key.UP) {
// do if key UP is pressed ..
} else if (this.keyCode==Key.DOWN) {
// do if key DOWN is pressed ..
} else if (this.keyCode==Key.ENTER || this.keyCode==Key.SPACE) {
// do if SPACE or ENTER is pressed ..
}
// Delete watching variable for perfomance
this.unwatch("this.keyPressedTime");
// Set keyCode to 255 = null
this.keyCode = 255;
}
Promixis
April 13th, 2004, 10:49 AM
Glad its working. Just wondering what you are trying to do?
MT3
April 14th, 2004, 01:03 AM
Glad its working. Just wondering what you are trying to do?
Again for everybody:
Flash has problems with the key-handle, if girder sends a key information.
Example: You have a flash movie with controllable buttons. If you pressed a key by keyboard, it selects the next button. If you send a key by girder, no buttons will be selected because it sends the code to fast for flash. Flash exist on single frames (default: 12 f/sec) and seemingly it runs his actionscript only a single time per frame. So its very improbable to get this fast girder key information while one frame just loading..
This script above is a workaround for this problem. It handles the keys by javascript. If sending/pressing a key, javascript will change the flash variables "keyPressedTime" to current timestamp and "keyCode" to last received key code.
In Flash/Actionscript there is a function "watch". It runs the function on parameter 2, if variable (parameter 1) has changed. That's it! You only need variable "keyCode" to get the key code for doing any actions..
Sorry for my bad english, i'm german. I hope you understand me now.
Or are there any questions?
Promixis
April 14th, 2004, 02:52 AM
Thanks for the explanation!
jpoveda
June 17th, 2004, 11:45 PM
Hi,
I have a problem with flash and keyboard and I think that should crrect it, but I don't understand very well what I have to do to. Can anybody explain me that a little more please?.
Thanks a lot
MT3
June 21st, 2004, 04:00 AM
Hi jpoveda!
I suppose you have a flash animation and a html page for including this flash animation, or?
Now we have the problem, hat Girder sends the key information to fast for flash. I have programming a work-around by using javascript (browser side programming language) and actionscript (flash programming language).
Put this javascript code between <head> and </head> in your html page:
<script type="text/javascript" language="JavaScript">
<!--
function setFlashVar()
{
var oEventTime = new Date(); // js-date object
var iEventTime = oEventTime.getTime(); // get timestamp in milliseconds
// Change flash variables (example for root-layer)
// "flashname" is the ID of your flash object (<object ..>)
document.getElementById("flashname").SetVariable("_root.keyPressedTime", iEventTime);
document.getElementById("flashname").SetVariable("_root.keyCode", event.keyCode);
}
//-->
</script>
The javascript function setFlashVar() saves the key information into flash variables.
Don't forget to set onkeydown="setFlashVar();" in body tag to execute the javascript function while one key is pressing:
<body onkeydown="setFlashVar();">
Now you have to include the flash animation. Example:
<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
onfocus="window.focus();"
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"
WIDTH="550" HEIGHT="400" id="flashname" ALIGN="">
<PARAM NAME=movie VALUE="flashname.swf">
<PARAM NAME=quality VALUE=high>
<PARAM NAME=bgcolor VALUE=#FFFFFF>
<EMBED
src="flashname.swf"
quality=high
bgcolor=#FFFFFF
WIDTH="550"
HEIGHT="400"
NAME="menu"
TYPE="application/x-shockwave-flash"
PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer">
</EMBED>
</OBJECT>
Don't forget to set onfocus="window.focus();" in object tag to be sure, that the flash animation is always active.
Ok, we have finished our html page. Now we edit our flash animation.
At best you create a new layer and call it for example ActionScripts. Then you put following actionscript code into first keyframe:
var keyPressedTime = 0;
var keyCode = 255;
this.onEnterFrame = function () {
// Watch the variable keyPressedTime
this.watch("this.keyPressedTime", this.execFunction(this.keyCode));
// If keyPressedTime change their value execute the function on second parameter
}
function execFunction(keyCode) {
this.keyCode = keyCode;
if (this.keyCode==Key.UP) {
// do if key UP is pressed ..
} else if (this.keyCode==Key.DOWN) {
// do if key DOWN is pressed ..
} else if (this.keyCode==Key.ENTER || this.keyCode==Key.SPACE) {
// do if SPACE or ENTER is pressed ..
}
// Delete watching variable for performance
this.unwatch("this.keyPressedTime");
// Set keyCode to 255 = null
this.keyCode = 255;
}
The javascript (from html page!) sends the key information to variable keyCode. The time of pressing it sends to variable keyPressedTime.
The flash function watch() checks the variable keyPressedTime. If the value changes, it starts the function execFunction() with the keyCode variable.
Now you have the pressed key information in the function execFunction.
Create "blank frames" from frame 2 till the end position of your flash animation, to be sure that the code executes on every frame position.
At final you have to conform your buttons / movieclip. I don't know what you have created in your flash animation. Hmm.. We make an example with a textfield.
Add a new layer and create a textfield, call it keyoutput.
We edit our actionscript:
function execFunction(keyCode) {
this.keyCode = keyCode;
if (this.keyCode==Key.UP) {
_root.keyoutput.value="Key UP is pressed..";
} else if (this.keyCode==Key.DOWN) {
_root.keyoutput.value="Key DOWN is pressed..";
} else if (this.keyCode==Key.ENTER || this.keyCode==Key.SPACE) {
_root.keyoutput.value="Enter or Space-Key is pressed..";
}
// Delete watching variable for performance
this.unwatch("this.keyPressedTime");
// Set keyCode to 255 = null
this.keyCode = 255;
}
Write instead of _root the name of the movieclip, button or an other scene of your flash animation if the text field is not in the root layer.
Finish! Now you can start your flash animation and press a key.
If hope this helps..
Regards,
Michi
jpoveda
June 23rd, 2004, 05:42 AM
Let me explain my problem better. I have a media center PC with ONLY a remote control. I'd like to play with basic games like asteroids, pacman, etc ... This games need flash to run, and when I assign keys to girder events I assign my remote control buttons to left, right, fire ..... but when I play with them I need to press thousand times left button to turn left a little my plane. I think that's because flash exist on less frames that I need.
So I need to send space, left or right keys to asteroids proccess by other way as a script f.e. and I don't know how to do it.
I'm sorry for my english and let me know if you need more information.
Thanks a lot for your support.
MT3
June 28th, 2004, 12:52 AM
Hmm.. Do you have .fla files of your flash games? Or are there only .swf files?
You need .fla file to edit your flash animation or add actionscripts..
jpoveda
June 28th, 2004, 01:29 AM
Hi,
Thanks again. I have a executable only (Asteroids.exe f.e.).
MT3
June 28th, 2004, 02:40 AM
You can't edit an executable program file. Sorry, but i can't help you..
quixote
July 13th, 2004, 06:27 PM
I know nothing of Flash, but I would just like to know if it would be possible to create some nice menus of different choices such as light #1 on, Light #2 on etc. with animations and effects, then have them receive commands from girder to react to choices made by the user. ie- if Light #1 is chosen with a girder event, then the menu choice "Light #1" lights up like a light bulb. These menus would need to be stand alone, that is, not accessed through a browser for simplicity's sake. Is this possible?
MT3
July 14th, 2004, 02:52 AM
Sorry, i couldn't understand you exactly.
If you mean to create flash menus and rollover the buttons or start an animation/effect by girder events - Yes!
Powered by vBulletin® Version 4.1.8 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.