Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 1998-03-05
5
Medium Priority
?
811 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 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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.
Suggested Courses

916 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