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)

Similar Messages

  • Is there a way to detect two keys at the same time?

    Is there a way to detect the keys if the user is holding two keys down at the same time?

    yes, but you'll have to check outside of the event loop (i.e. don't check for it in the keyPressed method).
    just use an array of keys that keeps track of which keys are currently down and which are up. you could use a boolean array but I use an int array because... Im not sure I've just done it probably because of my C background. anyway, basic setup:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    public class MultiKey extends JFrame {
         int keys[];
         public MultiKey() {
              super();
              keys = new int[256];
              Arrays.fill(keys,0); // 0 = key is up
              enableEvents(AWTEvent.KEY_EVENT_MASK);
              show();
         public void processKeyEvent(KeyEvent e) {
              // the 0xff below binds the key code to below 256
              int key = (e.getKeyCode()&0xff);
              if (e.getID() == KeyEvent.KEY_PRESSED) {
                   keys[key] = 1; // 1 = key is down
              else if (e.getID() == KeyEvent.KEY_RELEASED) {
                   keys[key] = 0; // 0 = key is up
         protected boolean isKeyDown(int key) {
              return (keys[key] != 0);
         protected boolean isKeyUp(int key) {
              return (keys[key] == 0);
    }so now at any time in your code you can check key state by using the isKeyUp and isKeyDown methods. this is about as close as you get to input polling in java unless you use LWJGL or something similiar ;)
    you can try out a full executable example at my site here - just open up the jar and start holding keys down :)

  • How to detect any key pressed for program running in the background?

    Hi All,
    is it possible to detect when any key is pressed if a java program is not in the focus? For example, when I have a console based program I can write something like:
    System.in.read();
    and wait until any key is pressed. However, if I move the mouse away from the console and make any other window active, the console program "doesn't hear" anything. Ok, it's clear, of course, but how is it possible to make the program detect any keys pressed even if it's running in the background?
    Thanks a lot for any hints!

    qnx wrote:
    1) Stop trying to make spyware. -> I don't do anything like this!
    2) Stop re-posting the same questions. -> didn't know they are the same, sorry.
    3) Stop taking us for fools. -> what? Honestly, I don't think that I realy deserved this :)With a limited posting history and the type of questions you are asking they are unusual.
    There are very few legitimate problem domains that would require what you are asking about. There are illegitimate ones though. And the legitimate ones would generally require someone with quite a bit of programming experience and would also be required (the fact that java can't do it would not get rid of the requirement.)
    Thus one might make assumptions about your intentions.

  • I want to detect a key press

    To detect a key press, i know i have to use the keylistener class
    public class key implements KeyListener{
         public static void main (String args[]){
              addKeyListener(this);
              keyReleased(KeyEvent e) {}
              keyTyped(KeyEvent e) {}
              keyPressed(KeyEvent e) {
    I built a simple program but it doesn't compile, it has an error.
    All i want is to detect a key press and do something for key presses...
    What am i doing wrong?

    I once got the following demo file somewhere in the Sun/Java site, probably in one of the link cited above..
    * Swing version
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    public class KeyEventDemo extends JApplet
                              implements KeyListener,
                                         ActionListener {
        JTextArea displayArea;
        JTextField typingArea;
        static final String newline = System.getProperty("line.separator");
        public void init() {
            JButton button = new JButton("Clear");
            button.addActionListener(this);
            typingArea = new JTextField(20);
            typingArea.addKeyListener(this);
            displayArea = new JTextArea();
            displayArea.setEditable(false);
            JScrollPane scrollPane = new JScrollPane(displayArea);
            scrollPane.setPreferredSize(new Dimension(375, 125));
            JPanel contentPane = new JPanel();
            contentPane.setLayout(new BorderLayout());
            contentPane.add(typingArea, BorderLayout.NORTH);
            contentPane.add(scrollPane, BorderLayout.CENTER);
            contentPane.add(button, BorderLayout.SOUTH);
            setContentPane(contentPane);
        /** Handle the key typed event from the text field. */
        public void keyTyped(KeyEvent e) {
            displayInfo(e, "KEY TYPED: ");
        /** Handle the key pressed event from the text field. */
        public void keyPressed(KeyEvent e) {
            displayInfo(e, "KEY PRESSED: ");
        /** Handle the key released event from the text field. */
        public void keyReleased(KeyEvent e) {
            displayInfo(e, "KEY RELEASED: ");
        /** Handle the button click. */
        public void actionPerformed(ActionEvent e) {
            //Clear the text components.
            displayArea.setText("");
            typingArea.setText("");
            //Return the focus to the typing area.
            typingArea.requestFocus();
         * We have to jump through some hoops to avoid
         * trying to print non-printing characters
         * such as Shift.  (Not only do they not print,
         * but if you put them in a String, the characters
         * afterward won't show up in the text area.)
        protected void displayInfo(KeyEvent e, String s){
            String charString, keyCodeString, modString, tmpString;
            char c = e.getKeyChar();
            int keyCode = e.getKeyCode();
            int modifiers = e.getModifiers();
            if (Character.isISOControl(c)) {
                charString = "key character = "
                           + "(an unprintable control character)";
            } else {
                charString = "key character = '"
                           + c + "'";
            keyCodeString = "key code = " + keyCode
                            + " ("
                            + KeyEvent.getKeyText(keyCode)
                            + ")";
            modString = "modifiers = " + modifiers;
            tmpString = KeyEvent.getKeyModifiersText(modifiers);
            if (tmpString.length() > 0) {
                modString += " (" + tmpString + ")";
            } else {
                modString += " (no modifiers)";
            displayArea.append(s + newline
                               + "    " + charString + newline
                               + "    " + keyCodeString + newline
                               + "    " + modString + newline);
    }

  • 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

  • Detecting asyncronous key press

    Hi all, I'm trying to figure out whether there is any way to detect an asynchronous key press for a simple and line-oriented java task.
    I mean no GUI, AWT and alike.
    All examples I could see refer to AWT events.
    The goal is to break long and time consuming processes upon pressing (for example) ctrl-c like we used to to in C++.
    Thanks.

    Word is not a JVM mastered process.
    I'm referring to what it seems the standard behavior of the JVM. I can be notified about ctrl-c, but I cannot change its standard action of terminating the JVM process.
    I tried by catching SIGINT using sun.misc.* - it works fine, handler is called - however upon returning (even without calling the previous handler - if any) the process exits.
    I don't see any way to change this - which was my primary goal.

  • 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.

  • How can I Connecticut two Airport Express with One Time Capsula?

    Only by Wifi or by LAN in a row?

    What are you trying to accomplish with the AirPort Express devices?
    Where will the Express devices be located in relation to the Time Capsule?
    If you are planning to use the Express devices to "extend" the wireless range of your network, Ethernet LAN will always provide far better performance than trying to connect the Express devices using wireless only.
    You have the option of connecting both AirPort Express devices directly back to two LAN <-> ports on the Time Capsule....or....connecting one Express back to the Time Capsule and then connecting the second Express to the first Express using Ethernet.  This assumes that you have a "new" Express with both WAN and LAN ports.
    If you choose wireless only, both Express devices will connect back directly to the Time Capsule, so both of them should be located where they can receive a strong signal from the Time Capsule wireless.
    A "hybrid" approach can also work. Connect one Express to the Time Capsule using an Ethernet cable and then setup the second Express to connect to the first Express using wireless only.

  • Detecting a key pressed anywhere

    How do you detect a key that is pressed no matter what window is selected?

    System.in.read()Because Windows only has Canonical mode for its DOS input,
    this always blocks which may not be what you want.
    see the example I posted earlier.
    http://forum.java.sun.com/thread.jspa?threadID=664543&messageID=3892105#3892105
    (T)

  • Two Mac computer in one Time Machine ? Possible?

    Hi
    Does anyone know that can I back up two computers in one Time Machine External HD? I have one Imac (PPC) and one MAcbook (intel). For both of them I'd like to make one Time machine HD. Does it work like that or do I need to back up two seperate TM HD for each of my computers?
    Many thanks...

    tur,
    There are multiple options for backing up 2 machines to one external drive. Remember that Time Machine is smart enough to recognize different machines and stores the backups in different folders.
    1: Connect your external drive to your main machine and then use Personal File Sharing to share that drive on your network. Keep in mind sharing the drive over the network will degrade network speed while Time Machine is working (Not such a big deal if you use a wired network).
    2: (The option I use) Run Time Machine on the first computer, eject it the drive, and then plug it onto the second machine and run time machine on that computer.
    I use option 2 because I have a machine I use as a media center and do not need regular backups of. I only back it up when I add files.
    A third option I have not had any experience with is using Apple's new Time Capsule.
    I hope this answers your question.

  • Unable to send two person sms at one time in ios 8.1.3

    if i send one person at one time so it easily sent but when i send two persons in a new message box so it say message sending failed
    either i if i send them same message separately so is sent.???
    Can you check what is the issue .?
    i am having this issue when i have updated it to 8.1.3..

    Hi shezi007,
    Thank you for contributing to the Apple Support Communities.
    It sounds like you're not able to send a group text message on your iPhone.
    You can find many good troubleshooting tips that can help the situation at this link:
    If you can't send or receive messages on your iPhone, iPad, or iPod touch - Apple Support
    I recommend starting with these tips:
    Go to Settings > Messages and make sure that iMessage, Send as SMS, or MMS Messaging is turned on (whichever method you're trying to use).
    If you can't send messages to a certain recipient, delete any conversations you have with them and try sending a new message. When you delete a conversation, you won’t be able to recover it. Make sure that you take a screenshot or forward any messages that you want to keep.
    If you can't send or receive SMS or MMS messages, contact your carrier to check your text messaging plan.
    Sincerely,
    Jeremy

  • Using InputMap and ActionMap to detect any key pressed...plz help

    Hi,
    I am quite new to Java. I have a tiny problem. I wanna put an actionlistener into a JTextField, so it printout something whenever a key is pressed (when the textField is selected).
    I can only get it to print if I assign a specific key to pressed...example below (works):
    JTextField searchBox = new JTextField();
    InputMap imap = searchBox.getInputMap();
    ActionMap amap = searchBox.getActionMap();
    imap.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0), "pressed");
    amap.put("pressed", new AbstractAction() {
              public void actionPerformed(ActionEvent e) {
                   System.out.println("TYPEEEEEEEEEEEEEEE");
    HOWEVER, when i change keystroke to any key pressed, it doesn't work. Example below(doesn't work):
    JTextField searchBox = new JTextField();
    InputMap imap = searchBox.getInputMap();
    ActionMap amap = searchBox.getActionMap();
    imap.put(KeyStroke.getKeyStroke(KeyEvent.KEY_PRESSED, 0), "pressed");
    amap.put("pressed", new AbstractAction() {
              public void actionPerformed(ActionEvent e) {
                   System.out.println("TYPEEEEEEEEEEEEEEE");
    Somebody plz helps me. I am really confused how to use KeyEvent.KEY_PRESSED. I thought it would work the same way as KeyEvent.VK_A...if i can't use KEY_PRESSED for that purpose, what KeyEvent do I use?
    Thank you very much in advance.

    Sounds like you want a KeyListener.
    textField.addKeyListener(new KeyListener() {
        public void keyTyped(KeyEvent e) {
            System.out.println("Key typed.");
        public void keyPressed(KeyEvent e) {
            System.out.println("Key pressed.");
        public void keyReleased(KeyEvent e) {
            System.out.println("Key released.");
    });See the API documentation for more information.

  • How to detect space key pressed event

    I am working on a japplet where it is needed to pause the applet on the occurrence of a space bar key pressed event can anybody help
    thanks in advance

    Have a look at the tutorial:
    http://java.sun.com/docs/books/tutorial/uiswing/events/keylistener.html
    http://java.sun.com/docs/books/tutorial/uiswing/misc/keybinding.html
    -Puce

  • Detecting a Key Press

    I am using this code to set up a listener for a key press. I
    noticed that it works fine when you use a LEFT, RIGHT etc. press,
    but it will not work with a regular key press(i.e. ctrl+P). It says
    that there is an undefined property as far as the Letter goes. If
    anyone can help me with this, I would very much appreciate it.
    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
    function keyDown(event:KeyboardEvent):void
    if(event.ctrlKey)
    var key = event.keyCode;
    var P_key = Keyboard.P;
    switch(key)
    case P_key:
    trace("this thing works");
    break;
    }

    Ok, I've figured it out. Flash decided to take me for a fool
    and pretended like it would take the P-key as a press, but in
    actuality, it only works that way in AIR.

  • Can I back up two different computers in one Time Capsule?

    I have two 15" MacBook Pro Intel Core 2 Duo, a first generation and a more recent one linked to a 500G Time Capsule.
    First question:
    Can I back up the two computers in the TC in separate "partitions", as in separate units like storage #1 and storage #2, so that the data from each computer is only stored in that assigned location? 
    Second question
    In case that should be doable, will it be also possible for each computer to access the data stored in either storage #1 or storage #2?

    Second question
    In case that should be doable, will it be also possible for each computer to access the data stored in either storage #1 or storage #2?
    The best way to see the backups for a different Mac is via the Browse Other Time Machine Disks option, per #17 in  Time Machine - Frequently Asked Questions.

Maybe you are looking for

  • Turning off the Driving Mode Announcement.

    I turned on driving mode on my Lumia 928 and now everytime I get into the car it announces: "I have turned on Driving Mode for you."  It has become real tiring.  How do you turn it off???  I have searched with no luck, please help. dwf1234

  • Date validation on selm screen

    Hi On selection selection screen user has selection field DATE by default sy-datum. i.e 6.10.2008 Now I should validate it i.e user can enter only last five days i.e .6.10.08 to 2.10.08 If user enters 1.10.08 he should get an error on selection scree

  • Create pdfs that perform calculations

    Hello. I would like to find a product in order to create beautiful pdf files, with text fields where you can enter numbers and perform various calculations with them. I can attach a pdf like the one I would like to create. Which product should I buy?

  • About the voip issue being not available(not a sol...

    Hi Friends I have formatted my phone.Reinstalled the firmware and  installed sip_voip3.0 settings v1 (yes v1) With v2 it was bouncing me of the menu but this time the menu was available.I did the settings and i saw that my phone was connected.There w

  • Iphone camera is not combining the light and dark photos but is presenting them as separate photos

    Hello Community, My friend showed me his iPhone 4 today and every time he takes a photo a dark version and a lighter version are shown, not combined as one but as two separate photos. I believe I remember that the camera takes more than one shot and