Solved

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

Posted on 1998-03-05
5
776 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Options in Zimbra Server 2 47
oracle 11g 23 51
Oracle SQL syntax check  without executing 6 38
how to see all occupied ports on windows 10 laptop 15 26
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now