Java know how much (94) keyboard events

Source: Internet
Author: User

Event sources for keyboard events are typically hack component related, and keyboard events occur when a component is activated, pressing, releasing, or tapping a key on the keyboard. The interface for keyboard events is KeyListener, and the method to register the keyboard event monitor is Addkeylistener (monitor). There are 3 implementations of the KeyListener interface:

    • keypressed (KeyEvent E): A key on the keyboard is pressed;
    • keyreleased (KeyEvent E): A key on the keyboard is pressed and released;
    • keytyped (KeyEvent e): A combination of two methods for keypressed and keyreleased.


The class that manages keyboard events is KeyEvent, which provides methods:
public int getKeyCode (), gets the key code that is pressed, the key table is defined in the KeyEvent class, see appendix.

The "Example 11-20" applet has a button and a text area, which acts as the source of the event where the keyboard event occurred and monitors it. When the program runs, click the button first to activate the button. When you enter an English letter later, the input letter is displayed in the body area. When the letters are displayed, the letters are separated by a space character and are filled with 10 letters, and the lines are displayed.

1 Importjava.applet.*2 Importjava.awt.*;3 Importjava.awt.event.*;4  Public classExample6_10extendsAppletImplementskeylistener{5     intCount =0;6Button Button =NewButton ();7TextArea Text =NewTextArea (5,20);8      Public voidinit () {9Button.addkeylistener ( This);Ten Add (button); Add (text); One     } A      Public voidkeypressed (KeyEvent e) { -         intt =E.getkeycode (); -         if(t>=keyevent.vk_a&&t<=keyevent.vk_z) { theText.append ((Char) t+ ""); -count++; -             if(count%10==0) -Text.append ("\ n"); +         } -     } +      Public voidkeytyped (KeyEvent e) {} A      Public voidkeyreleased (KeyEvent e) {} at}

Series Articles:

Java know how much (top)Java know how much (medium)Java knows how many () Java vectors (vector) and their applicationsJava know how much (79) hash table and its applicationJava know how much (80) graphical Interface design basicsJava know how much (81) frame window BasicsJava know how much (82) Introduction to tags, buttons, and button eventsJava know how much (83) Panel Basics: JPanel and JScrollPaneJava know how much (84) layout design of graphical interfaceJava know how much (85) text box and text areaJava know how much (86) input and output of text box and text areaJava know how much (87) Select boxes and radio buttonsJava know how many (88) lists and combo boxesJava know how many (89) lists and combo boxesJava know how much (90) menuJava know how much (91) dialog boxJava know how much (92) scroll barJava know how much (93) mouse Events

Java know how much (94) keyboard events

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.