DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2005
    Posts
    24

    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();
    }

    }

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    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.

  3. #3
    Join Date
    Nov 2005
    Posts
    24
    yeh it does!

    im new to java programming, code u help on how i should code it?

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    You responded quickly! I've been editing my reply - it has some suggested implementation in it, now ...

  5. #5
    Join Date
    Nov 2005
    Posts
    24
    hey man! what d i use for toggle?

    a variable? like x??? :s or jus write toggle?

  6. #6
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    how about

    boolean toggle = false;

  7. #7
    Join Date
    Nov 2005
    Posts
    24
    thankyou

Similar Threads

  1. Disable keyboard, keep mouse working
    By Andrew in forum VB Classic
    Replies: 7
    Last Post: 06-25-2009, 10:55 PM
  2. Replies: 1
    Last Post: 09-11-2005, 02:02 AM
  3. listening to keyboard events
    By guny in forum Java
    Replies: 4
    Last Post: 04-06-2005, 01:25 PM
  4. Disable keyboard, keep mouse working
    By Andrew in forum VB Classic
    Replies: 0
    Last Post: 04-28-2000, 10:33 PM
  5. Capturing keyboard events
    By Brian Leung in forum VB Classic
    Replies: 0
    Last Post: 04-11-2000, 09:14 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


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


Sponsored Links