Solved

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

Posted on 1998-03-05
5
774 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
Comment Utility
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
Comment Utility
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
Comment Utility
But where do I get the Component source, int id, and long when from?  What values are these?
0
 

Author Comment

by:bjg
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
commonTwo  challenge 63 98
how to use external config file with Spring MVC 4 59
json format text only 4 64
HashMap Vs TreeMap 12 47
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

772 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

12 Experts available now in Live!

Get 1:1 Help Now