-
How to detect SHIFT key in action events
I want to detect whether the SHIFT key is held down when a button is pressed.
Here is what I am doing:
JButton butt = new JButton("Click me!");
butt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if ( (e.getModifiers() & ActionEvent.SHIFT_MASK) != 0)
JOptionPane.showMessageDialog(null, "SHIFT key is pressed.");
else
JOptionPane.showMessageDialog(null, "SHIFT key is not pressed.");
}
});
While testing this code, what I find out is that e.getModifiers() always
returns 0 and hence the message "SHIFT key is not pressed." is always displayed.
How do I enable the tracking of keys such as SHIFT, CNTRL or ALT ?
Any help is appreciated.
Minh.
-
Re: How to detect SHIFT key in action events
Key events arnt distributed toward the action listeners, have to use and declare
KeyListener separate and just register with a bool in same class that shift
has been pressed and released.
Anon
"Minh" <java.@127.0.0.1> wrote:
>
>I want to detect whether the SHIFT key is held down when a button is pressed.
> Here is what I am doing:
>
>JButton butt = new JButton("Click me!");
>
>butt.addActionListener(new ActionListener() {
> public void actionPerformed(ActionEvent e)
> {
> if ( (e.getModifiers() & ActionEvent.SHIFT_MASK) != 0)
> JOptionPane.showMessageDialog(null, "SHIFT key is pressed.");
> else
> JOptionPane.showMessageDialog(null, "SHIFT key is not pressed.");
> }
>});
>
>While testing this code, what I find out is that e.getModifiers() always
>returns 0 and hence the message "SHIFT key is not pressed." is always displayed.
>
>How do I enable the tracking of keys such as SHIFT, CNTRL or ALT ?
>
>Any help is appreciated.
>Minh.
>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks