A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: How to pass a keycode that has not been pressed?

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Location
    San Diego, CA
    Posts
    3

    Question How to pass a keycode that has not been pressed?

    Not even my professor knew this one, so maybe it isn't possible, but here goes...

    I have a function that needs to pass the "spacebar" keycode into an array. I'm not sure how to give the function a keycode that hasn't actually been pressed. It seems like there should be a way to do this, but I haven't been able to figure it out. Is there a way to give a keycode (which is interpreted as such, rather than as a number) to a function, without that key having been pressed?

    I appreciate any help you can offer.

    RC

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    A keycode IS an int. You can create your own KeyboardEvent instance with the keycode you want and have whatever object you want dispatch it, or just call the listener with that fake event.

    I'm confused about what the difficulty is here.

    Maybe if you posted some code, like the function which needs to get the keycode. Is that the same function as the KeyboardEvent listener?

  3. #3
    Junior Member
    Join Date
    Nov 2009
    Location
    San Diego, CA
    Posts
    3
    Hi, thanks for the reply,

    I don't have the code with me at the moment, but this is what's going on;

    It's a kind of Hangman style game where the user has to guess a password. I have it working with single words, but when I use multiple words in the string, the spaces get detected as characters, and the user must hit the spacebar in order to win.

    I have a function which takes user input and adds it to an array. Future input is checked against this array to make sure the user isn't just hitting the same key over and over. I thought if I could put the spacebar keycode into the array at the start, it could solve the issue where the user must hit the spacebar in order to win. (because it would already be in the array and counted as "hit".

    The problem is, I know how to pass a keycode from a keyboard event, but I don't know how to pass one when no keyboard event had occurred. If the user hasn't hit the key, then how do I tell the function to run it?

    Could you show me an example of how to make the fake keyboard event you mention?

    Thanks again,
    BTW, if you find somewhere that gives beer for moderating, let me know

    RC
    Last edited by RC_FlashKit; 11-16-2009 at 08:57 PM.

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You don't need to fake an event for this. You just need to put the right value into the array like you wanted to to begin with.

    Are you putting characters in the array, or keycodes?

    I'd suggest putting characters. In fact, use toLowerCase, or toUpperCase so it doesn't matter if the user puts in 'a' or 'A'. Then just put ' ' in your array from the start.

    If you're putting keycodes into your array, then you'll need to find the keycode for space. Just trace out the keycodes in your program, and press space. I don't recall what it is off the top of my head.

  5. #5
    Junior Member
    Join Date
    Nov 2009
    Location
    San Diego, CA
    Posts
    3

    Solution

    Thanks for your help.

    The problem was, I was forgetting that arrays can take mixed data types. So it was fine to have the function receiving both keycodes and characters. I finally just used keycodes for the user input, and string characters (" ", in the case of the spacebar) for the coded input.

    Works fine, thanks again,
    RC

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I wouldn't suggest mixing the types in an array. Then you've got to handle two different types in whatever is processing that array. Instead, just put in the keycode for space. I believe the code is 32.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center