-
Keyboard events working apart from...
HI ALL!
I am using the KeyListener to invoke methods when a key is pressed! it all works fine, but i wanted to add an "if" statement to say if "Q" is pressed then only active the other Keyevents..
For example NUMPAD 8 is used to Zoom into the image and NUMPAD 2 is used to zoom out, now i dont want the user to be able to press these and get output unless they have pressed the Q key... ive made an example code below to show what i mean kinda!!!
im thinking it may be wrong because if the user presses the Q key again then i wish to come out of that loop and disable the NUMPAD buttons again and so on.... counter maybe?? help!!!
Heres the code so far....
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_Q){
if (e.getKeyCode() == KeyEvent.VK_NUMPAD8) {
m_imagePanel.zoomIn();
adjustLayout();
}else if (e.getKeyCode() == KeyEvent.VK_NUMPAD2){
m_imagePanel.zoomOut();
adjustLayout();
}else if (e.getKeyCode() == KeyEvent.VK_NUMPAD5){
m_imagePanel.originalSize();
adjustLayout();
}
}
-
So you want to use the Q key event to be a toggle. If you press Q, it changes the value of the toggle, which is either on or off ...
Sounds like a boolean value to me:
Code:
if (e.getKeyCode() == KeyEvent.VK_Q) {toggle = !toggle};
then for your other key events, test, for example:
Code:
if (toggle && e.getKeyCode() == KeyEvent.VK_NUMPAD2){}
Last edited by nspils; 12-14-2005 at 09:24 AM.
-
yeh it does!
im new to java programming, code u help on how i should code it?
-
You responded quickly! I've been editing my reply - it has some suggested implementation in it, now ...
-
hey man! what d i use for toggle?
a variable? like x??? :s or jus write toggle?
-
how about
boolean toggle = false;
-
Similar Threads
-
By Andrew in forum VB Classic
Replies: 7
Last Post: 06-25-2009, 10:55 PM
-
By me_code2004 in forum VB Classic
Replies: 1
Last Post: 09-11-2005, 02:02 AM
-
Replies: 4
Last Post: 04-06-2005, 01:25 PM
-
By Andrew in forum VB Classic
Replies: 0
Last Post: 04-28-2000, 10:33 PM
-
By Brian Leung in forum VB Classic
Replies: 0
Last Post: 04-11-2000, 09:14 AM
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