2 key pressing at a time?

i am currently making a java application that require 2 key pressing a the same time with KeyPressed(KeyEvent e)but it doesnt seem to work.does any body know how to do it? thanks

This is only supported with the "meta" keys (Control, Shift and Alt). Look at the KeyEvent for the methods to determine which meta key was pressed in combination with the other key.

Similar Messages

  • Detecting two keys press at one time

    i need to do a event handler, say when user press shift + enter, it will do something, else enter would do another thing
    i tried but i can't get the event handler to detect two key press, how do I go about it?

    VK_ENTER vs (VK_ENTER + SHIFT_DOWN_MASK)

  • How do you clear the buffer of excess key presses, in Java?

    Right now, in the game I am working on, if the user presses a key multiple times the actions will be performed as many times as it registers, even though I disallow key presses during the time that the action is taking place.
    For example, a move of 1 space can be carried out with a key press. While the character is moving, key presses are not allowed. However, if you just press the key 5 or 25 times, the character will just keep moving, once he's done with the first one.
    What I need to do is clear the buffer of key presses after an action has taken place, so that the excess keys are not registered. I know the commands to do so in C++, but not in Java.

    Well, I think you've got what I was saying backwards, or I'm not understanding what you are asking correctly.
    it is suppposed to return if isMoving is true, not false. If isMoving is true, it means that the moving animation, and actions are taking place, Which is a process that take about a second, for the player to move one space.
    Here's the code where I process the keypresses
    private void processKey(KeyEvent e) {
        if (isMoving) {return;}
        int keyCode = e.getKeyCode();
        if (!isPaused && !gameOver) {
            isMoving = true;
          if (keyCode == KeyEvent.VK_UP) {
            player.move(player.NE);
          else if (keyCode == KeyEvent.VK_DOWN) {
            player.move(player.SW);
          else if (keyCode == KeyEvent.VK_LEFT) {
            player.move(player.NW);
          else if (keyCode == KeyEvent.VK_RIGHT) {
            player.move(player.SE);
            isMoving = false;
    }And in case I need to clear up what I want to happen, and the results I'm seeing. The player, in this case, moves one space in a direction, with a single press of an arrow key. That functionality is working fine, as intended. However, I don't want you to press the key twice quickly and move two spaces. I don't want you to hold down an arrow key and move continually.
    That part of the functionality is what is not working.
    As I understand my code, pressing the key once should set isMoving to true and, therefore further presses shouldn't register until isMoving is false again, which shouldn't happen until the player moves have finished.
    But, the way that it is working now is that if I press the key 2, 3 or 20 times, as soon as one move is finished another begins. Are the key presses just sitting in the buffer somewhere waiting for isMoving to become false again? And if so, is there a function I can call to clear that buffer after a move is finished?

  • Amount of key presses a user makes over time - BPM

    Hi,
    Im working on a small program that needs to get the amount of keypresses a user makes over time.
    Basically its for calcuating the BPM a user is inputting. So a uers taps a beat for 5 seconds and the amount of tap is saved and multiplied by 12 to get the Users BPM.
    So far i have this:
    package Version1;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.util.Timer;
    import java.util.TimerTask;
    import javax.swing.*;
    public class muTap extends JFrame implements KeyListener, ActionListener {
        JTextArea display;
         * main - the main method
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    theGUI();
         * muTap constructor
        public muTap(String name) {
            super(name);
         * theGUI - creates the gui for muTap
        public static void theGUI() {
            muTap frame = new muTap("muTap");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //Set up the content pane.
            frame.addComponents();
            //Display the window.
            frame.pack();
            frame.setVisible(true);
         * adComponents - adds gui components to muTap
        public void addComponents() {
            JButton button = new JButton("Start");
            button.addActionListener(this);
            display = new JTextArea();
            display.setEditable(false);
            display.addKeyListener(this);
            JScrollPane scrollPane = new JScrollPane(display);
            scrollPane.setPreferredSize(new Dimension(375, 125));
            getContentPane().add(scrollPane, BorderLayout.PAGE_START);
            getContentPane().add(button, BorderLayout.PAGE_END);
        public void keyTyped(KeyEvent e) {
            //Do Nothing
        /** Handle the key pressed event from the text field. */
        public void keyPressed(KeyEvent e) {
            //TODO call the getBPMfromKboard() method
            taps++;
            System.out.println("Tap: " + taps);
        /** Handle the key released event from the text field. */
        public void keyReleased(KeyEvent e) {
            //Do Nothing
        /** Handle the button click. */
        public void actionPerformed(ActionEvent e) {
            //Do Nothing
            //initialCountdown(3, "Start Tapping Fool");
            countDown(3);
         * countDown - a simple countdown timer for use when getting users input.
         * @param time amount of time to count down from
         * @return      true when timer ends.
        public static int taps;
        public void countDown(final int time) {
            taps = 0;
            final Timer timer = new Timer();
            timer.scheduleAtFixedRate(new TimerTask()
                int i = time;
                public void run()  {
                    i--;
                    String s = Integer.parseInt(i); // error because of this i.
                    display.setText(s);
                    if (i < 0) {
                        timer.cancel();
                        if(time == 3)
                            display.setText("Start Tapping");
                            countDown(5);
                        else if(time == 5)
                            display.setText("Number of taps: " + taps);
                            //System.out.print("Number of taps" +taps);
            }, 0, 1000);
    }But i get errors when i try running it:
    "Exception in thread "Timer-1" java.lang.RuntimeException: Uncompilable source code"
    It doesnt seem to like me parsing int to string here:
    String s = Integer.parseInt(i); Any ideas? or if youve got suggestions on another way to do it it'd be much appreiated.
    Thanks!

    Korvanica wrote:
    It doesnt seem to like me parsing int to string here:
    String s = Integer.parseInt(i);
    Yikes, you've got that bit of code backward, there partner.
    You use Integer.parseInt to parse a String into an int, not the other way around.
    If you want to change an int to a String, do
    String s = String.valueOf(i)or you can use the various formatters out there.

  • Can't press more than 2 directional arrows keys at the same time

    I just noticed that in the Macbook Pro Retina that I bought recently there's no way to press more than 2 arrow keys at the same time, or at least the system does not seem to handle it.
    All other keys can be pressed at the same time without any problems, so I think that the problem affects only the arrow keys.
    I'm surprised that in a modern computer can exist this type of limitation
    I think Apple should update the system to allow this - car gamers like me will thanks or feel less disappointed...
    Any help would be appreciated!
    Thanks
    GM

    @kalos
    Uninstalled everything PA, also uninstalled mangler which was the only thing that depended on PA. It didn't work anyway.
    No pulseaudio --kill command so I couldn't run.
    Checked systemd journal and there is no mention of PA this time.
    Checked top, and grepped ps aux and there is no PA there either.
    I also rebooted the machine completely just to make sure. However VLC and HTML5 have the same incompatibilities.
    _ vlc Videos/anime/fmab/01.mp4
    VLC media player 2.1.5 Rincewind (revision 2.1.4-49-gdab6cb5)
    [0x1bc30f8] pulse audio output error: PulseAudio server connection failure: Connection refused
    [0x1af4118] main libvlc: Running vlc with the default interface. Use 'cvlc' to use vlc without interface.
    Fontconfig warning: FcPattern object size does not accept value "0"
    Fontconfig warning: FcPattern object size does not accept value "0"
    Fontconfig warning: FcPattern object size does not accept value "0"
    Fontconfig warning: FcPattern object size does not accept value "0"
    [0x7fb604001248] main vout display error: Failed to resize display
    But the video and audio did work until I ran some HTML5 video, after which the audio worked, and then I opened it again and it didn't work...
    @nomorewindows
    I see. That's too bad. At least we're not alone.
    Last edited by Greduan (2014-11-14 01:34:29)

  • F1 Key seems to be constantly pressed all the time but it is not the key

    My Envy DV7 was getting too hot so I decided to open it up and clean the fan myself ( After I cleaned the fan, It was very difficult for me to close it back, because the keyboard on this Pc is attached to the whole top so I had to take it to a laptop shop, so they can close it for me) When I finally got to close it the F1 key seemed to be pressed all the time when it is actually not, the thing is that it only happens when it first boots up after a while the laptop works just fine.

    It should only scan when you select the AirPort drop down menu. Anytime you do this it is going to scan for wireless networks in case you want to connect to one that has come online or gone offline since the last scan. That doesn't mean it is always scanning, it just means it is scanning anytime you use the drop down menu. This is normal.

  • Key press elapsed time

    I'm building a shooting game. I want to record how long the player has the F key pressed in order to use that value to remove ammunition sprites from the stage.
    My application loops in frame 1.
    I have a script that subtracts the key press time from the system time on key release. I think the repeat rate of the keyboard is interfering with this as the output figures are quite random and holding the key down for longer returns a lower number.
    global gFire, gTotal, gStartTime
    on startMovie
      the keyDownScript = "handleKeydown"
      the keyUpScript = "handleKeyUp"
    end startMovie
    --Handler to fire
    on handleKeydown
      if the key = "f" then
        gStartTime = the milliseconds
      end if 
    end handleKeydown
    --Handler to stop firing
    on handleKeyUp
      if the key = "f" then
        gfire = (the milliseconds - gStartTime)/10
        gLeft = gTotal - gFire
        gTotal = gLeft -- gTotal is used to incrementally turn off sprite visibility
        put gfire -- Trace elapsed time to Message window
      end if
    end handleKeyUp
    --This sequence removes bullets from the stage\
    according to he time spent firing
    on enterFrame
      if tTotal < 900 then
        sprite(10).visible = False --this repeated for a sequence of 10 sprites
      end if
    end
    The other problem with this is that it doesn't update the stage till the key is released, so a player can defeat the logic by just holding the key down continuously (especially because the value of gFire doesn't increase as expected)
    Ideally I'd like to record the elapsed time the key is down, add it to a global and have it update the stage at the frame rate, so I guess it would have to be within a frame event handler.
    Any help would be gratefully received.

    MStewart,
    Thanks for doing that. I decided to do a little something like that and just reworked it a bit. Dropped the pause on a case structure and put the timer inside. seemed easier to do it that way for what I wanted to accomplish. The pause is going to be a good thing to have in the event that the heater needs to be turnd off and worked on for any reason, and it won't accrue time or cycles. Used a "in range and coerce" to compare minutes and seconds and then wired a 4 to 15 mA simulated signal to to the timer. All I got to do now is duplicate it 14 more times and then to work on the cDaq part of it to control relays through dig out signals, and sequence the whole thing to write a file for the analog in. Thanks alot for your help and BowenM too. like I said: you guys are awesome. Got me to thinking in other directions.
    Attachments:
    Mexico sentinal loop.vi ‏11 KB
    milliamp timer w pause function.vi ‏24 KB

  • Ctrl is pressed all the time in Photoshop

    Sometimes Photoshop thinks the Ctrl key is pressed all the time. This prevents me from doing most of the stuff you want to do without Ctrl.
    Currently happening on Photoshop CS5 12.0.4/Mac OS 10.6.7. But I had this issue for about half a year with Photoshop CS2 and Mac OS 10.6.x.
    Restarting Photoshop does not help. The only way out is restating Mac OS. This does not effect other applications.
    I had this issue right after booting and also after doing a lot of other stuff.
    I’m using a Wacom tablet and an Apple wireless keyboard—which may send keyboard signals.
    Any idea?

    Currently happening on Photoshop CS5 12.0.4/Mac OS 10.6.7
    Absolutely sure on that OS version? Symptom is very similar to an OS 10.6.5 -10.6.6 Apple bug.

  • How to defer the primary key validation to commit time

    Hi
    Is it possible to defer the primary key validation to commit time? I don't know why the framework checks for the unique key constraint immediately after inserting the row and before committing it. This causes "Too many objects match the primary key oracle.jbo.Key[null]" error if the user presses the create new record button multiple times before filling and saving the previous records.
    Thanks,
    Ferez

    Dear M.Jabr,
    Many thanks for your reply. I have access to the database but I prefer to use an ADF workaround to this problem rather than a DB workaround. I am not sure but I think that there should be a way to defer or disable primary key constraint in ADF.
    Anyway, I tried to make the primary key constraint DEFERRABLE in DB using PL/SQL developer but an error occurred (the name is used by another object) which I don't know why.
    Thanks,
    Ferez

  • Detecting multiple key presses

    Using the code below I am trying to detect keypresses for a java applet which involves holding down of several keys at once. I have 2 problems, probably related. Firstly I can only detect 3-4 simultaneous keys being held down and secondly the keyPressed and released! events are continually fired while a key is held down not just when it was first pressed as you would think. I would like to be able to detect up to a maximum of 16 keys being held down at once, is this possible? I am working on a unix system in x-windows which I think accounts for the auto-repeat problem, but why am I having trouble detecting multiple simultaneous keys being held down?
    It there a boolean function I can call to check the state of given keys at a given time, that would be very useful?
    Any help is appreciated.
    Ian
    public void keyPressed(KeyEvent e) {
    int keyCode = e.getKeyCode();
    switch(keyCode){
    case java.awt.event.KeyEvent.VK_O: //o
         odown=true;
    break;
    case java.awt.event.KeyEvent.VK_P: //p
         pdown=true;
    break;
    case java.awt.event.KeyEvent.VK_Q: //q
         qdown=true;
    break;
    case java.awt.event.KeyEvent.VK_W: //w
         up=true;
    break;
    case java.awt.event.KeyEvent.VK_A: //a
         left=true;
    break;
    case java.awt.event.KeyEvent.VK_S: //s
         down=true;
    break;
    case java.awt.event.KeyEvent.VK_D: //d
         right=true;
    break;
    }//method keypressed
    public void keyTyped(KeyEvent e) {
    }//method keytyped
    public void keyReleased(KeyEvent e) {
    int keyCode = e.getKeyCode();
    switch(keyCode){
    case java.awt.event.KeyEvent.VK_O: //o
         odown=false;
    break;
    case java.awt.event.KeyEvent.VK_P: //p
         pdown=false;
    break;
    case java.awt.event.KeyEvent.VK_Q: //q
    qdown=false;
    break;
    case java.awt.event.KeyEvent.VK_W: //w
         up=false;
    break;
    case java.awt.event.KeyEvent.VK_A: //a
         left=false;
    break;
    case java.awt.event.KeyEvent.VK_S: //s
         down=false;
    break;
    case java.awt.event.KeyEvent.VK_D: //d
         right=false;
    break;
    }//method keyReleased

    Did you ever find a solution to this problem. I am experiencing a similar problem. I want to be able to disable the auto-repeat function of the keyboard, so that I can tell if the user is holding down the key, or pressing it several times.
    If you have found a solution please let me know at [email protected]
    Thanks,
    Ben Newton

  • Multiple Key Pressing

    When I hold Tab + w space doesn't work click (I have my controls set as this for a game).
    Is there anyway to make it so i can hold those two keys and have spacebar work?
    Im on a MacBook Pro Retina 13 inch with latest version of yosemite
    Thanks for the help

    Here is an example of both multiple key press and repeat delay ignore. It is a terrible example of animation. Do not us this animation loop in a game. It will flicker far too much.import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class Test extends JFrame implements Runnable {
      boolean upPressed=false, downPressed=false, leftPressed=false, rightPressed=false;
      public Test() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        addKeyListener(new KeyAdapter() {
          public void keyPressed(KeyEvent ke) {
         switch (ke.getKeyCode()) {
           case KeyEvent.VK_UP: upPressed = true; break;
           case KeyEvent.VK_DOWN: downPressed = true; break;
           case KeyEvent.VK_LEFT: leftPressed = true; break;
           case KeyEvent.VK_RIGHT: rightPressed = true; break;
          public void keyReleased(KeyEvent ke) {
         switch (ke.getKeyCode()) {
           case KeyEvent.VK_UP: upPressed = false; break;
           case KeyEvent.VK_DOWN: downPressed = false; break;
           case KeyEvent.VK_LEFT: leftPressed = false; break;
           case KeyEvent.VK_RIGHT: rightPressed = false; break;
        setSize(250,250);
        setVisible(true);
        Thread timer = new Thread(this);
        timer.start();
      public static void main(String[] args) {
        new Test();
      public void run() {
        while (true) {
          repaint();
          try { Thread.sleep(30);} catch (Exception e) {}
      public void paint(Graphics g) {
        g.setColor(Color.white);
        g.fillRect(0,0,getWidth(), getHeight());
        g.setColor(Color.black);
        g.drawString("Up",40,40);
        if (upPressed) g.drawString("Pressed",100,40);
        g.drawString("Down",40,60);
        if (downPressed) g.drawString("Pressed",100,60);
        g.drawString("Left",40,80);
        if (leftPressed) g.drawString("Pressed",100,80);
        g.drawString("Right",40,100);
        if (rightPressed) g.drawString("Pressed",100,100);
    }

  • Key pressed in Jlist and selecting the item of key list accordingly

    Hi,
    I have a JList with the items in sorted order.Now I want that if a person presses any key (say K) then the first item starting with K should be selected.Hmmm I can do it by addding a key listener to the list and cheking all the items in the list ,by traversing through the whole lenght of JList and selecting the item if it starts with the character of the key pressed.
    But i was thinking if there is any better way to do that?
    Regards Amin

    see bugid: 4654916 - it does say that the the
    scrolling for the JList should now work with keyboard
    selection.I have the same problem. Thanx for the hint with the bugid. Saw a good workaround there with a simple subclass of JList. Works for me although it is annoying to subclass JList all the time. The bug seems not to be fixed in the 1.4.1 JDK.
    Andreas

  • Catching Alt Key Press with the Key Down Filter Event

    I am writing an application that requires specific key combinations using ctrl, shift, and alt in addition to a function key (F1, F2, F3, etc).  The application works great except for when I try to catch an alt key press.  The alt key press does not seem to fire an event eventhough it is an option in the PlatMods cluster as well as the VKey enum.  When I press the alt key when my application is running the cursor changes to a normal mouse pointer from the usual finger pointer and prevents any other key presses from going through (in addition to not firing an event itself).
    I have tried completely removing the run-time menu, which doesn't seem to help.  I currently discard all keys after I handle them in my event structure.
    I really hope that the only solution isn't using a Windows DLL.  Any suggestions or ideas at all would be greatly appreciated.
    Thanks,
    Ames

     Hi Ames
    As Kileen has said Khalid has already given you a good solution to detect the ALT key.
    I have another approach that might let you stick to your event-driven approach. I suggest that you have another loop in your app that polls the keyboard using the Input Device utility vi's. When this poll loop sees an ALT + KEY combo it raises a dynamic user event and will be processed in your event structure. This means you can keep your key down filter event to process the CTRL + KEY and SHIFT + KEY events.
    Example attached in 7.1
    cheers
    David
    Attachments:
    Catching Alt Key Press Poll with Events(151551).vi ‏89 KB

  • "Enter Passcode" screen not showing the dots for key press

    I've a passcode that is 6 characters long. When I slide to unlock my phone , the "Enter Passcode" screen comes up as normal, but sometimes when I try to enter my passcode in the white box, though the key press turns blue under my finger tips as normal but nothing (no black dots) is entered into the white box. Whenever this happens I'm not able to unlock my phone with the passcode and I've to power off and power on the phone to detect the passcode.
    Not sure anyone else having the same problem as mine. If anyone encountered the same problem and know how to fix it, please help me.
    Thanks!

    I have two iPhone 4S both with iOs7 and simple passcode. One had the dots, the other didn't. I went through changing settings on the one without dots to match the settings to the one with dots one at a time until they came on.
    For me the exact change was turning on the Auto Brightness setting.
    Settings > Wallpapers and Brightness > Auto-Brightness
    Perhaps it's the same bug for complex passcodes? Worth a try.

  • Wish you could tap the Shift key to set next key press in caps on iMac

    I really wish in Mac OS that you could 'tap' the Shift key to set up the next key press in capitals.  In Lion, on my iMac desktop, the present arrangement requires you to hold down the Shift key while hitting the letter you want to capitalise, as has been the case since time began.  On my lovely iPad2, you just tap the shift key once and the next keypress is in caps - SIMPLE and efficient.  I have been yearning for this simple change to the OS since way before the iPad came along, but now you can use it in iOS5 and NOT in Lion, it's really frustrating.  i find i am now failing time after time to select caps in Lion...
    Please, oh wise ones at Apple: take a few minutes out from whatever fresh ramification of iCloud you are concocting and giVe uS bETter cAps.

    There is this, but it might do more than you want:

Maybe you are looking for