www.webdeveloper.com
Results 1 to 6 of 6

Thread: capturing keyboard events fired outside application window

  1. #1
    Join Date
    Oct 2004
    Location
    The very depths of programming hell
    Posts
    828

    capturing keyboard events fired outside application window

    Hey everyone. I've made a test file to remind me of how to make a graphic stand-alone application, and has some basic features too.


    • It displays the current coordinates of the mouse
    • It displays the keyCode and what the character is (if number or letter) of the last key event fired
    • It changes the colour between three values on the left, middle, and right clicks


    The code is below:

    Code:
    import javax.swing.JFrame;
    import java.awt.*;
    import java.awt.event.*;
    
    
    public class GraphicsTest extends JFrame
    implements MouseListener,MouseMotionListener,KeyListener
    {
    Color b=new Color(25,25,25),c=new Color(200,200,200);
    final int Y_ZERO=31;
    final int X_ZERO=5;
    
    Color arch;
    
    int keyyer=-1,mX=-1,mY=-1;
    
    Graphics backstage;
    Image buffer;
    
    boolean keyOn=false;
    
    public static void main(String[] args)
    {
    new GraphicsTest();
    }
    
     public GraphicsTest()
      {
      super("GraphicsTest");
      
      addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent we){System.exit(0);}});
      addMouseListener(this);addMouseMotionListener(this);addKeyListener(this);
      
      setSize(500,100);
      setVisible(true);
      
      buffer=createImage(size().width,size().height);
      backstage=buffer.getGraphics();
      
      generateScreen();
      }
    
     public void generateScreen()
      {
      setBackground(b);
      backstage.setColor(b);
      backstage.fillRect(0,0,size().width,size().height);
      backstage.setColor(c);
      backstage.drawString("This shows how application graphics works",20+X_ZERO,20+Y_ZERO);
    //-------------------------------------------------------------------------------------------------->>>>  
      if(mX!=-1&&mY!=-1)
       backstage.drawString("Mouse co-ordinates: "+(mX-(X_ZERO-1))+","+(mY-(Y_ZERO-1)),20+X_ZERO,35+Y_ZERO);
      if(keyyer!=-1)
       {
       if((keyyer>=48&&keyyer<=57)||(keyyer>=65&&keyyer<=90))
        backstage.drawString("The code for the last key typed is "+keyyer+", which is the "+letOrNum(keyyer<60)+" \""+(char)keyyer+"\"",20+X_ZERO,50+Y_ZERO);
       else
        backstage.drawString("The code for the last key typed is "+keyyer+", which is a special key or character",20+X_ZERO,50+Y_ZERO);
       }
      repaint();
      }
    
     public void paint(Graphics g)
     { 
     g.drawImage(buffer,0,0,this);
     }
    
    public String letOrNum(boolean gB){if(gB==true){return "number";}else{return "letter";}}
    
    public void mouseReleased(MouseEvent m){}
    public void mousePressed(MouseEvent m){}
    public void mouseEntered(MouseEvent m){}
    public void mouseExited(MouseEvent m){}
    public void mouseClicked(MouseEvent m)
     {
     mX=m.getX();mY=m.getY();
     if(m.getButton()==m.BUTTON1)
      {
      c=new Color(255,0,0);
      }
     if(m.getButton()==m.BUTTON2)
      {
      c=new Color(0,255,0);
      }
     if(m.getButton()==m.BUTTON3)
      {
      c=new Color(0,0,255);
      }
     generateScreen();
     }
    public void mouseMoved(MouseEvent m)
     {
     mX=m.getX();mY=m.getY();
     generateScreen();
     }
    public void mouseDragged(MouseEvent m){}
    
    
    
    
    public void keyTyped(KeyEvent k)
     {
     
     }
    public void keyPressed(KeyEvent k)
     {
     
     }
    public void keyReleased(KeyEvent k)
     {
     backstage.setColor(c);
     keyyer=k.getKeyCode();
     generateScreen();
     }
    
    }

    I want to know if it's possible to detect key and mouse events fired OUTSIDE the application window.


    Thanks for your help.
    >I suck at this game, can you give me some pointers?

    >> 0x278AB4FF, 0xB5822BB4, 0xAD6E673D.

    > I hate you.


  2. #2
    Join Date
    Mar 2004
    Posts
    3,056
    you don't think that kind of thing could fall under the "surreptitious and nasty" umbrella of software?

  3. #3
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    5,298
    To my knowledge, its not possible as Java doesnt know or care what is outside of its own frame.

    I may well be wrong.

  4. #4
    Join Date
    Oct 2004
    Location
    The very depths of programming hell
    Posts
    828
    you don't think that kind of thing could fall under the "surreptitious and nasty" umbrella of software?
    Only if used in the wrong way. I do not intend to make anything malicious.
    >I suck at this game, can you give me some pointers?

    >> 0x278AB4FF, 0xB5822BB4, 0xAD6E673D.

    > I hate you.


  5. #5
    Join Date
    Mar 2004
    Posts
    3,056
    Sorry, I wasn't inferring that you're up to something you shouldn't be. Just that maybe it wouldn't work on most peoples' systems because if their firewall saw anything like that going on it'd put the kaibosh on it straight away.

  6. #6
    Join Date
    Oct 2004
    Location
    The very depths of programming hell
    Posts
    828
    im only making applications for my own use at the moment. so, that wouldn't be a problem
    >I suck at this game, can you give me some pointers?

    >> 0x278AB4FF, 0xB5822BB4, 0xAD6E673D.

    > I hate you.


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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



Recent Articles