Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 1998-03-05
5
Medium Priority
?
805 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 100 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.
Suggested Courses

722 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