Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

KeyListener not working for ONE particular key!  RSS feed

 
aruna makhija
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,
I wrote a program to capture key events:
However, it works for every single key on my keyboard EXCEPT for the PRINTSCREEN KEY, for which it generates only the KeyReleased Event, and not the KeyPressed event!
code follows:
-----------8<---------------<br /> <pre><br /> import java.applet.Applet;<br /> import java.awt.Component;<br /> import java.awt.Graphics;<br /> import java.awt.event.*;<br /> public class KeyDemo extends Applet<br /> implements KeyListener<br /> {<br /> private String keyLine;<br /> private String actionLine;<br /> private String modifierLine;<br /> public KeyDemo()<br /> {<br /> keyLine = "Press any key and see what happens";<br /> actionLine = "";<br /> modifierLine = "";<br /> }<br /> public void init()<br /> {<br /> addKeyListener(this);<br /> }<br /> public void keyPressed(KeyEvent keyevent)<br /> {<br /> keyLine = "Key " + KeyEvent.getKeyText(keyevent.getKeyCode()) + " pressed.";<br /> setAction_Modifiers(keyevent);<br /> }<br /> public void keyReleased(KeyEvent keyevent)<br /> {<br /> keyLine = "Key " + KeyEvent.getKeyText(keyevent.getKeyCode()) + " released.";<br /> setAction_Modifiers(keyevent);<br /> }<br /> public void keyTyped(KeyEvent keyevent)<br /> {<br /> keyLine = "Key " + keyevent.getKeyChar() + " typed.";<br /> setAction_Modifiers(keyevent);<br /> }<br /> public void paint(Graphics g)<br /> {<br /> g.drawString(keyLine, 25, 25);<br /> g.drawString(actionLine, 25, 40);<br /> g.drawString(modifierLine, 25, 55);<br /> }<br /> private void setAction_Modifiers(KeyEvent keyevent)<br /> {<br /> actionLine = "Action key: " + (keyevent.isActionKey() ? "Yes" : "No ");<br /> String s = KeyEvent.getKeyModifiersText(keyevent.getModifiers());<br /> modifierLine = "Modifier keys: " + (s.equals("") ? "[none]" : s);<br /> repaint();<br /> }<br /> }<br /> ------------------------>8----------------------
HTML APPLET Snippet:
<applet code="KeyDemo.class" align="baseline" codebase = "." <br /> width="354" height="600"></applet>
</pre>
===============================================
Anybody, any clues!?
Aruna
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!