Solved

How can u set multiple modifiers on java.awt.KeyEvent

Posted on 1998-03-05
5
785 Views
Last Modified: 2008-02-01
How can you set multiple modifiers on a java.awt.KeyEvent.

For example:

public void keyPressed(KeyEvent e)
{
    if (e.getKeyCode() == KeyEvent.VK_TAB)
    {
        I want the KeyEvent to be Ctrl+Shift+Tab, how do I do this.
        I've tried e.setModifiers(KeyEvent.VK_CONTROL &
KeyEvent.VK_SHIFT),
        but that doesn't work.
}

0
Comment
Question by:bjg
  • 2
  • 2
5 Comments
 
LVL 5

Expert Comment

by:msmolyak
ID: 1233441
I am not sure what you mean by "setting modifiers". Key event is generated when a key is pressed. You can get modifiers used to generate that event (they are defined in InputEvent class), but I am not sure whether this is what you are asking about.
0
 

Accepted Solution

by:
XLogger earned 50 total points
ID: 1233442
First of all, if you'd like to combine multiple modifiers, you should use '|' (binary OR) rather than '&' (binary AND) operation.

The second, VK_xxx constants defined in KeyEvent class are virtual codes of keys and not
intended  to be used with any binary operation. Instead, as correctly noticed msmolyak,
you should use xxx_MASK constants from InputEvent class.

If I correctly understood your question, you want artificially "fire" key event. So one possible way to do it is following:

  ...
   e.setModifiers( InputEvent.ALT_MASK | InputEvent.CTRL_MASK );
  ...

Note: I don't know what JDK version d'you use, but in JDK 1.1.4, function 'setModifiers'
is deprecated, so consider using constructor of KeyEvent to generate new key event
with required parameters.

0
 

Author Comment

by:bjg
ID: 1233443
But where do I get the Component source, int id, and long when from?  What values are these?
0
 

Author Comment

by:bjg
ID: 1233444
Also, why should I use the OR | rather than the AND &?  I want to set all of the modifiers, so why use OR rather than AND?
0
 

Expert Comment

by:XLogger
ID: 1233445
1) "Component" is an object which "fires" event. You should select which  object will play this role in your class hierarcy.

     "id" is event id. There are predefined constants in each xxxEvent class. For key event it may be KeyEvent.KEY_PRESSED, KeyEvent.KEY_RELEASED or KeyEvent.KEY_TYPED. Simply choose one of them.

    "when" the third "long" parameter is so called "event time stamp" - when the event occured,
you may set it to 0 unless you need it. Or if you'd like completly emulate event generation,
set it to value of 'System.currentTimeMillis ()' function which returns current time in milliseconds.

As for second part of  your comment you'd better refer any document concerning binary logic - it's too large area to explain here.
  Just to give you general idea: logical operators such as &&, || and ! are not like binary operators &, |, ~. The latter operator set works with sets of bits. Here's the tables for them:

&: 0&0=0,     0&1=0,     1&0=0,     1&1=1
|:   0|0=0,     0|1=1,     1|0=1,     1|1=1
~:  ~0=1,     ~1=0

Let's assume that ALT_MASK=1(binary1),  CTRL_MASK=2(binary 10)
so,
  ALT_MASK & CTRL_MASK gives you 0
while
  ALT_MASK | CTRL_MASK gives you 3(binary 11)

if you try to check resulting event afterwards, in former case it will be as if neither
ALT nor CTRL were pressed. Instead, everything will work correctly with the second one.

---------------
Ufffffffffffffffff.
I hope this time I completly satisfied your curosity and deserved my points:),
   My best regards
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question