-1

Hi, I wish to know why the AND operator is not working here? Even though without operator it working fine what is wrong while including it?

else if (keyCode == KeyEvent.KEYCODE_CTRL_LEFT && keyCode == KeyEvent.KEYCODE_S) {
    int visibility=new_Sell_order_cart.getVisibility();
    if(visibility==View.VISIBLE)
    {
        openCart(); 
    }
}

If its not possible how jQuery developer Achive this

        if (e.keyCode == 65 && e.ctrlKey) {
        alert('ctrl A');
    }
});
Kailas
  • 7,350
  • 3
  • 47
  • 63
DJhon
  • 1,548
  • 3
  • 22
  • 39
  • 2
    are u looking for OR operator? AND will return false when either or both of the statements are `false`... – Gopal Gopi Jun 11 '14 at 05:33
  • 5
    How do you expect `keyCode` to be equal to two different things? – PakkuDon Jun 11 '14 at 05:33
  • Unless you have a quantum computer, keyCode cannot have two different values at the same time – Amir Afghani Jun 11 '14 at 05:34
  • I want to perform a task with click on LEFT_CTRL and s both. – DJhon Jun 11 '14 at 05:34
  • @BlueGreen Then go for OR operator... – Gopal Gopi Jun 11 '14 at 05:35
  • @GopalRao,, if i used OR then it will work s or crlt but i want only for ctrl+s only – DJhon Jun 11 '14 at 05:36
  • 2
    @BlueGreen: I haven't done Android programming before but I'm assuming that you're getting the keycode from a KeyEvent, correct? Wouldn't you want to check another property of the event such as its [meta state](http://developer.android.com/reference/android/view/KeyEvent.html#getMetaState()) as well? – PakkuDon Jun 11 '14 at 05:38
  • hope [this](http://stackoverflow.com/a/14079494/2764279) helps you – earthmover Jun 11 '14 at 05:40

2 Answers2

1

keyCode cannot be two keys at once. It can be either CTRL_LEFT or KEYCODE_S.

This statement returns false because either one will be true but not both.

(Its like a==3 && a==4, a cannot be 3 and 4 at the same time)

keyCode == KeyEvent.KEYCODE_CTRL_LEFT && keyCode == KeyEvent.KEYCODE_S

Probably you are looking for OR (||) operator:

 else if (keyCode == KeyEvent.KEYCODE_CTRL_LEFT || keyCode == KeyEvent.KEYCODE_S) {

EDIT:

If you want to check for two keypress. Here is the solution suggested in this post.

One way would be to keep track yourself of what keys are currently down.

When you get a keyPressed event, add the new key to the list; when you get a keyReleased event, remove the key from the list.

Then in your game loop, you can do actions based on what's in the list of keys.

Community
  • 1
  • 1
Bipin Bhandari
  • 2,694
  • 23
  • 38
0

I got one Solution , I know it is not up-to mark but it full-fill my requirement .

       else if (isCTRLisPressed(17) && keyCode == KeyEvent.KEYCODE_S) {
        int visibility=new_Sell_order_cart.getVisibility();     
        if(visibility==View.VISIBLE)
        {
            openCart(); 
        }
    }     

and

   public boolean isCTRLisPressed(int i){
if(i==17){
    System.out.println(" key code of ctrl"+i);
    return true;
}
else {
    System.out.println("key code in else "+i);
    return false;
}
}
DJhon
  • 1,548
  • 3
  • 22
  • 39
  • So whats the use of AND operator here? directly u are hardcoding here and returning `true` when value is 17... so you are condition now becomes `if(true && keyCode == KeyEvent.KEYCODE_S)` where `&&` operator is useless... – Gopal Gopi Jun 11 '14 at 10:10
  • Something like that,,,that's why i told "not up-to mark" – DJhon Jun 11 '14 at 10:49