Simulating key strokes

SquareNote

  • Posts: 20
I know it's possible to have a behavior set the x and y of the mouse, but is it possible to have a behavior simulate the pressing of a key?  I know this wouldn't make sense in most game contexts but I have some ideas for interfacing between the game and external devices using GlovePIE, which would listen for key presses.  I would want the key presses to be able to come from the game itself in addition to the player.

Any thoughts?

Hectate

  • *
  • Posts: 4643
I don't think it is, security issue with falsifying user key press data. AFAIK Flash just "listens" for key presses from the OS and reacts appropriately according to it's code. There's no way for Flash to tell the OS itself that a key has been pressed.

I could be wrong though, I'd check the Flixel forums and other flash forums and websites to see if anybody's gotten something like that working.
:
:
Patience is a Virtue,
But Haste is my Life.
Proud member of the League of Idiotic Stencylers; doing things in Stencyl that probably shouldn't be done.

Silux

  • Posts: 438
You can set Events which trigger messages and make the game do the same thing as a key is pressed.
You can't still have the keyboard of the player move itself.

EX:
Every 2 seconds
 send 'X key pressed'
If 'X' key is pressed or this hears 'X key pressed'
 Do this
Currently working at:
Starwarrior 2097(my main project)
How to make successful games in Kongregate and the world(article)

Hectate

  • *
  • Posts: 4643
Yes, initially I was going to suggest that. All you have to do is encapsulate any actions that take place with a key press inside of a message block and just send the messages to fake a key press (or when one is actually pressed). Unfortunately he is trying to send key presses from Flash to a separate piece of software, GlovePIE (I don't know what it does).
:
:
Patience is a Virtue,
But Haste is my Life.
Proud member of the League of Idiotic Stencylers; doing things in Stencyl that probably shouldn't be done.

SquareNote

  • Posts: 20
I didn't think of the security angle, but yeah, that makes sense. I'll poke around on the Flixel forums to see, but I won't get my hopes up.

GlovePIE is a great little piece of hobby software that can convert a ton of different control inputs to different control outputs. You can use it for things like connecting your WiiMote to your PC via BlueTooth and mapping the pointer, the buttons, and gestures to keystrokes and mouse movement.

What I was thinking specifically was to use GlovePIE to convert a keystroke coming from the Flash game into a MIDI command for a MIDI sequencer.

Hectate

  • *
  • Posts: 4643
A second option would be to see if there's something that hooks into GlovePIE and allows you to capture the keystrokes without interrupting them from getting to Flash.?
:
:
Patience is a Virtue,
But Haste is my Life.
Proud member of the League of Idiotic Stencylers; doing things in Stencyl that probably shouldn't be done.

SquareNote

  • Posts: 20
It's not a question of capturing keystrokes, it's more like generating keystrokes that are initiated by the game itself rather than the player. Basically I'm trying to get SOME kind of information out of my game in a way that can ultimately be interpreted by GlovePIE.

I asked around on the Flixel forums and they said that AS3 can communicate with JavaScript using ExternalInterface and I can potentially use that. So have the game send something to the JavaScript which then simulates a key event that GlovePIE translates to something else.

Except I can't find any info about ExternalInterface in the Stencyl docs so I'm not sure it's supported here. I only barely understand how all of this fits together, so maybe I'm looking in the wrong place.

I think I might be out of luck on this one.

DoctorMikeReddy

  • *
  • Posts: 180
bump

I'd like to know how to use ExternalInterface too...

Hectate

  • *
  • Posts: 4643
Here's the best place to start learning.

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/external/ExternalInterface.html?filter_flash=cs5&filter_flashplayer=10.2&filter_air=2.6

And here's a simple example found here
Code: [Select]
import flash.external.ExternalInterface;

// Calling a JavaScript function from AS3 (with optional parameters)
if (ExternalInterface.available) ExternalInterface.call("javascriptFunction", param1, param2);

// Implementing a JavaScript callback in AS3 (flashFunction is called when JavaScript calls javascriptFunction)
if (ExternalInterface.available) ExternalInterface.addCallback("javascriptFunction", flashFunction);
Of course, you'll also have to make some JavaScript with appropriate functions to call and return data with. And now I know as much as you do, in theory! :)
:
:
Patience is a Virtue,
But Haste is my Life.
Proud member of the League of Idiotic Stencylers; doing things in Stencyl that probably shouldn't be done.

coleislazy

  • *
  • Posts: 2607
ExternalInterface is part of Flash, not Stencyl, so it is supported, but not part of the Stencyl documentation.

Disclaimer: I have never used this interface and never tested it, but this is my understanding based of the official Adobe documentation.

To use this, you would first need to import flash.external.ExternalInterface. Then, to make a call to JavaScript, you would do like so:

ExternalInterface.call(my_function, args);

Here is the documentation:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/external/ExternalInterface.html

And some examples:
http://flex-blogger.com/blog/2010/12/13/flex-and-html-comunication-with-externalinterface/

DoctorMikeReddy

  • *
  • Posts: 180
That is useful, and better general sources than I found. However, I was wondering if there were any Stencyl specific examples. Wait a moment... This sounds like a Challenge...

Like http://community.stencyl.com/index.php/topic,2943.msg20059.html

OK here it is...
http://community.stencyl.com/index.php/topic,3292.new.html