KeyboardFocusManager dispatches only one event while multiple are fired

I'm writing the game Achtung die Kurve, or 'Zatacka' in Java.
It's in fullscreen and there are multiple players with their own controls.
Now, to catch the KeyEvents (controls) for each Player, the Player objects get created with the following line in the constructor:
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(this);However, when multiple players are holding (KEY_PRESSED) different buttons at the same time, only the 'last' one gets processed.
The following example shows my point:
import java.awt.GridLayout;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class main
    private static JTextArea txt;
    public static void main(String[] args)
        new main();
    public main()
        JFrame f = new JFrame();
        f.setSize(300, 700);
        f.setVisible(true);
        f.setDefaultCloseOperation(f.DISPOSE_ON_CLOSE);
        f.setLayout(new GridLayout());
        txt = new JTextArea();
        txt.setEditable(false);
        f.add(txt);
        Listener l1 = new Listener(KeyEvent.VK_Z);
        Listener l2 = new Listener(KeyEvent.VK_X);
    class Listener implements KeyEventDispatcher
        int ktlt;
        public Listener(int keyToListen)
            ktlt = keyToListen;
            KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(this);
        public boolean dispatchKeyEvent(KeyEvent e)
            if (e.getID() == e.KEY_PRESSED && e.getKeyCode() == ktlt)
                txt.append("" + ktlt + "\n");
                return true;
            return false;
    }}Try holding the 'z' and 'x' buttons at the same time (for a short period).
What you would expect is that the output will contain the key codes for both of the buttons, alternatively.
What you see, is that the textarea gets filled with only the 'last' key that is being pressed.
What am I doing wrong here?
Edited by: Tails on 12-jan-2009 21:22

Tails wrote:
That's kind of a problem as I am on Linux.As am I
KeyEvents are broken on linux: every key press action generates KEY_TYPED, KEY_RELEASED and KEY_PRESSED events at the same time, which means that's worthless...I got it to work for my purposes. What is the alternative?

Similar Messages

  • How to use only one event case at a time??

    Using LabVIEW, using the event structure is it possible to have only one event case execute at a time. For example, having event case controlled by keyboard, and the other event case controlled by mouse (clicking buttons) and only one being able to execute, not simultaneously. I was considering using a switch of some sort, but each case does not have a "stop if true/false" so I am open to a new method or work around. I attached code. I do not want output to be able to be sent from the keyboard case and mouse case at the same time.
    Thanks,
    Andrew
    Attachments:
    WiFi Motor Control & Cam Stripped.vi ‏29 KB

    Ah, OK. all you need is to keep state in a shift register that ensures that all "key down/mouse down" events are discarded after each key down/mounse down event until a key up or mouse up (resp.) has occured.
    Overall, you code is still way to complicated for this simple task. For example, you could eliminate all these "select" primitives and place the approriate diagram constant inside the respective case. On the other hand it is too simplistic, because you would think that certain key combinations should be allowed concurrently (e.g. up and left).
    What does the receiver actually do with the codes? Do you have any documentation for this device?
    LabVIEW Champion . Do more with less code and in less time .

  • Suddenly itunes shows only 5 songs while there are 550 songs in my iphone. I suspect it started after upgrade to ios7.1.1

    suddenly itunes shows only 5 songs while there are 550 songs in my iphone. I suspect it started after upgrade to ios7.1.1
    Izzy

    When you look at Setting > General > About does it show 550 in Songs? 
    Can you add songs to your phone from iTunes on your computer?
    I did an iOS update to one of the 6.X versions and it wiped out all my music and I had to add it back manually even though I has just done a backup to iTunes.  Restoring from that backup didn't help.  My music was all gone.  But since then I have not had any problems with the updates erasing my music.

  • HT1349 How do I set up a separate icloud account? Both my wife and I have an iPhone 4s and an iPad, but only one Mac. We are tired of our "stuff" getting mixed up together when we sync.

    How do I set up a separate icloud account? Both my wife and I have an iPhone 4s and an iPad (1 and 3), but only one Mac. We are tired of our "stuff" getting mixed up together when we sync. Thanks much

    You don't need separate iCloud accounts if you want to simply sync unique content to each device using iTunes. I have three iDevices using one ID and one iTunes library and each device has it's own content on it. You need to set up the sync configuration for each device. This is what I recommend.
    First turn off auto sync in iTunes. Launch iTunes on the Mac and go to iTunes>Preferences>Devices>Prevent iPods, iPhones and iPads from automatically syncing. Click OK and then quit iTunes.
    Connect one of your devices to the Mac and launch iTunes.
    Click on the device name on the left side under the devices heading.
    Click on a tab in iTunes - Apps for instance.
    Click on the Apps that you want to sync to that device.
    Make sure that the Sync Apps heading is checked.
    Follow the same procedure for all of the other tabs in iTunes ..... Music, Photos, Movies and so on.
    After you have finished making all of your selections, click on Apply in the lower right corner of iTunes.
    When you are done, eject the first device and do the same thing with the other device. iTunes will now remember the sync configuration on each device.
    You can change that configuration any time that you like by following the steps outlined above.
    If you want to continue to auto sync, turn it back on in the iTunes preferences as described above.

  • Flex4.5 SDK under Flash builder Burito doesnt dispatch Enter key event in Text Area anymore

    Flex 4.5 under Flash builder Burito doesnt dispatch 'Enter' key event in Text Area anymore, in prev version 4.1 it was working properly, this stopped working after migration to Burito + Flex SDK 4.5?
    What that suppose to be?

    Hi
    That's true that Flex4.5 SDK under Flash builder Burito doesn't listen Enter key in Text Area for KeyUp or KeyDown event.
    Here I found a work around you can use.
    Add an eventlistener to the component in actionscript and set the useCapture=true next to eventlistener function. Now you can listen enter key for KeyUp or KeyDown Event
    Hope this helps
    Rush-me

  • Only one of my fans are working on my macbook pro retina 13 inch late 2013 model. Is something wrong?

    It appears that only one of my fans are working on my macbook pro with retina display 13 inch late 2013 model. Is some thing wrong? if so how can i fix it?

    To my knowledge there is only one fan on all 13" Mac notebooks, Retina or standard screen. Not enough space for a second fan and no Discrete GPU to cool.
    So nothing is wrong.

  • I can't see my events from iPhoto (11 - 9.3.1). Only one event is in my library (the first one). The others events disapear!!

    I can't see my events from iPhoto (11 - 9.3.1). Only one event is in my library (the first one) and the last import that I made in iphoto is the first one . The others events disapear!! What can be happen? Please, someone could help me?

    I tried to do shift + alt + command - option
    That's not what I suggested...
    Hold down the option (or alt) key key and launch iPhoto.
    Restore from your back up is the easiest solution.

  • Logic 8 using only one CPU while bouncing

    Whenever I bounce a project, Logic 8 uses only one CPU. It also spikes this CPU completely, with the CPU meter going into the red area, which is why I am rather keen on persuading Logic to distribute the load.
    Any ideas?
    (I've given the support document with tips on how to balance multi-core performance a good read, but it doesn't seem to say anything on this particular subject, and following the advice in the document does nothing to change my CPU load.)

    Bouncing is a single core process. Just like rendering is in Final Cut Pro. This won't change until Leopard and Apple's ProApps get better at handling multiple cores.
    It's redlining because the processor core is working as fast as it can. Computer processors don't just randomly run slower, they use their resources for the tasks at hand. If it only used 50% of a core, the bounce would take twice as long to process.

  • I need a gaming keyboard for my mac but the only ones I can find are for Windows,  there is no Command button or FN button.  Any suggestions?

    So, I have an OS X 10.7.5 and I want to get a gaming keyboard, but the only good ones I can find are for windows.  I know because they have the Windows home key button, and no FN button or Command button.  Any suggestions?

    Normally, most Windows-compatible keyboards are also compatible with Mac OS X. If I'm not mistaken, the Windows key (the key with the Windows icon) is the command key. Also, if you do need to use the F1-F12 keys as standard function keys (you would normally hold FN and then press the button), you can set them to standard function keys in System Preferences. There may be some good gaming keyboards on the market that use the Mac layout, but most may use a Windows keyboard. Personally, I prefer the Mac keyboard on my Macbook. I hope this helps!

  • How do I restore an only one event from backup

    Ho do I restore one Event of many from Backup in Iphoto 9 (8.1.2)?

    Take a look at this link, https://discussions.apple.com/message/15417614#15417614

  • How do I add one event to multiple, but not repeating, dates

    I routinely need to add single events to 10 to 20 dates at once. These dates rarely line up in a repeating sequence. For instance I may need to schedule "appointment with John at 1pm" on M,Tu this week; W,Thu,Fri next week; M,Thu the following week. And "appointment with Jane at 10am" on M this week; Tu next week; Thu the following week.
    I know I can copy and paste identical text in one at a time on each date but this is tedious.
    Is there an app or trick I can use to do this efficiently? Previously I used an old version of Calendar Creator in which I could create an event and then push a toggle switch on all the days across a month in which the event would reoccur.
    Thanks! I'm new to Mac and iCal.

    Hello,
    Welcome to Apple Discussions.
    1. Make the first event in iCal when and how you want it.
    2. If you are not in month view, switch to it.
    3. Highlight the event in the calendar view and press Cmd-C (copy).
    4. Click on the blank area on a day you want to copy to.
    5. Press Cmd-V (paste).
    6. Repeat steps 4-5 for all needed dates.
    Best wishes
    John M

  • Is it possible to commit one table, while other are rolleded backed

    Hi to all,
    I am working on a report that calls a package which populates some tables dynmically, after the report is done it does a rollback.
    I want to capture the data it populates in one of the tables, I have created a temp table, a procedure which populates the data before it does a rollback.
    My question is, when the rollback happens will mydata from the table will be rollbacked, if I do a commit in the procedure which inserts into my temp table will the data populated in others be committed too, in which case it will create a lot of problems. I cannot see the code of the package as how many tables are getting populated.
    Can I possible do a commit just for my temp table?
    Thanks to all

    Thanks,
    Actually I created a procedure like this.
    PROCEDURE insert_temp_tables IS
    v_temp_fin_r3licrpt_count number;
    PRAGMA AUTONOMOUS_TRANSACTION;
    BEGIN
    SELECT COUNT(*)
    INTO v_temp_fin_r3licrpt_count
    FROM temp_fin_r3licrpt;
    IF v_temp_fin_r3licrpt_count > 0 THEN
    DELETE FROM temp_fin_r3licrpt;
    END IF;
    INSERT INTO temp_fin_r3licrpt
    SELECT *
    FROM fin_r3licrpt;
    COMMIT;
    END;
    I tested this in SQLPlus and it works fine it commits only the SQL in the transaction and all other statements remain uneffected. But the problem is that when I created the procedure in reports I got this error.
    Procedure Body: INSERT_TEMP_TABLES, 1 Error
    Error 127 at lines 5, column 8
    PRAGMA AUTONOMOUS_TRANSACTION is not a supported pragma
    we are using Oracle 6i reports, could this be problem.
    What was your suggestion by
    anonemous transaction procedure;
    Can you brief about this...
    Thanks again..
    Habeeb

  • So I'm not the only one whose purchased songs are cutting off/skipping

    This has been the most frustrating thing... I'm definitely not new to downloading music from the iTunes store, and I have no idea what's been going on lately with purchased tracks just stopping and skipping ahead to the next track. Thanks to all for the helpful hints on how to redownload the bum tracks by deleting them and going back into purchases. A couple of things I noticed after doing some troubleshooting:
    1. The only time I had issues with tracks was when I downloaded an entire album at the same time.
    2. While going through and finding the duds on an album with 60 tracks (there were 21 of them...), I noticed that in almost every case it was TWO back-to-back tracks that didn't download correctly. So I figure that there may've been some issue with them downloading simultaneously. That's where this tidbit I found on another discussion helps:
    3. Unchecking the "Allow Simultaneous Downloads" box in the downloads tab of iTunes cleared up EVERYTHING. I think...
    I hope this helps.

    You posted this in another thread...
    scratching, freezing, saying 'do not disconnect' for like 6 hours when I plug it into the computer even though itunes says 'ipod update is complete' and other annoying problems.
    Scratches...
    Yes, it will scratch. Again, get a case. Here are some ways to clean it...
    Ice Creme
    iCleaner
    Freezing...
    Try a restore (see my previous post)
    Do Not Disconnect...
    Try clicking the little eject icon next to the iPod in iTunes (under Source). iTunes should eject the iPod and Do Not Disconnect should go away.
    Do Not Disconnect usually stays after the iPod is done updating when Disk Use is enabled. To make the iPod automatically eject go to edit-->preferences with the iPod connected in iTunes. Click the iPod tab and then the Music tab under that. Uncheck enable Disk Use and click OK.
    Post back if disk use is not enabled/when you click eject iTunes says that it cannot eject the iPod.
    btabz

  • Can I have calendar alerts for only one calendar with multiple calendars?

    My wife has multiple calendars on her iPhone (mine and hers), but she would like to receive alerts/notifications only for the items on her calendar. Is this possible? (This seems to be the opposite problem from what this person experienced: http://discussions.apple.com/thread.jspa?messageID=8479193.)
    We sync with Outlook on a PC via iTunes.

    I have the very same problem. In our company, we have shared CalDAV calendars for every employee. I receive and sync those to my Mac and to my iPhone/iPod Touch; on both platforms I only want alerts for my personal appointments, and not to get alerted when my colleagues have a business lunch.
    Interestingly, in iCal on Mac OS X, there is an option "Ignore Alarms" for every single shared calendar, and I can cherry-pick those that I want to get alerted about. This is exactly the behavior I want on the iPhone, too, which regrettably is not possible (yet?).
    The other option would be to forbid alarms in shared calendars for everybody, which is not feasible, too, because people might want to get alarms without having to duplicate the date to some personal calendar first.
    So if anyone has a practical solution to this problem, I'd be happy to hear about it. Thanks!
    --Alex.

  • HT1206 we have only one computer and we are a family of four how do i keep my music apart from my kids music on itunes ? all of us have different id's and passwords but everytime we plug my iphone or their ipod touch we end up having the same apps ,games

    how do I keep my music on itunes apart from my kids? everytime I connect my ipod to my computer I end up with a lot of apps and all thier songs on my iphone.

    Do you mean you all have different AppleIDs?  But, do you also have different user accounts on the computer?
    Simplest thing is just give everyone their own user account on the computer - they log into their account and sync their devices from only their account.  Keeps everything separate and neat.

Maybe you are looking for

  • BW upgrade from 3.x to BI 7.0

    Hello BI experts, Could you please let me know how will we plan the BW upgrade? It would be better if somebody can give me steps. Answer will be appreciated and rewarded with full points. Thanks and regards, Harry

  • I am unable to unlock additional loops and sounds in 10.0.2 running Maverick

    I've tried every solution here and all filters are off. I still get the Message that I need to purchase the content when I click on the greyed out loops, then upon signing in, that I've already purchased the loops and sounds and should download them

  • Where's the FREAKING manual for the DigitalLife Security System?

    Where is the printed documentation for the AT&T DigitalLife Security System?  The so-called on-line system is PATHETIC and UNUSABLE, as it is impossible to(a) get it to run at all and (b) has light gray TINY printing on a white screen, making it UTTE

  • Updating KM Metadata properties using KM API

    Hi All, We are tring to update the custom Metadata properties using KM API from the Abstract Portal Component. We have written the below code for updating the metadata. We have checkbox called "Region" which has multiple option values like "Asia,Amer

  • Unlock pdf

    how can I unlock a pdf to answer questions using the computer?