Java AWT tracks Keyboard Event through KeyListener. The KeyListener will get the KeyEvent which discloses data of user interaction with the keyboard. For example, it holds what key is typed by the user. The KeyEvent is raised when the user pressed a keyboard key and it also raised when he/she released it. The Event also tells what key is typed by the user. When these actions take place, AWT reports the KeyEvent to KeyListener. The KeyListener receives the KeyEvent on exposed function which tells what action is done by the user. The handler functions exposed by the KeyListener are below: (1) keyPressed(KeyEvent e) (2) keyReleased(KeyEvent e) (3) keyTyped(KeyEvent e) Below picture shows the relation between KeyEvent and KeyListener:
In 80s, usually mouse came with only two buttons. One is for left click and other one is for right click. Now as days mouse comes with three buttons and a wheel in the middle. The wheel allows scrolling the contents, and also it allows click. Below is the picture of the modern days mouse.
In our example, we will have three panels and the AWT Frame hosts them via BorderLayout manger. The top panel hosts three AWT Scrollbar controls to denote red, blue, green color values. Middle panel will live in the centre part of the Frame Window. Gray Scale check box will sit in the South part of the Frame window’s border layout. All three AWT Scrollbar together represent an RGB color value. So, each Scroll can produce a range of values which falls between 0 to 255. When the user adjusts the Scrollbar, we will create RGB Color and apply that to the panel in the middle of the Frame. When Gray Scale is in checked state, we produce the grey-scaled color and apply that to the middle panel. This example helps you to learn how to use AWT Scrollbar and how to handle the AdjustmentEvent raised by it.
Here is the screenshot of the sample AWT window, which we will create in this example: In our example, we have a Multi-Select List box control in the upper left portion of the AWT Frame. When the user clicks the Get Fruits button, the AWT TextArea control will show all the selected list items in it. The Clear Output button will delete the text displayed in the AWT TextArea Control. This example will help you learn how to add strings on AWT List Control and how to retrieve all the items selected by the user. Now, we will proceed with the example.
The SDI sample, under the file menu, has two menu items called Hook CAPS Lock, Unhook CAPS Lock. When the user clicks the Hook Caps Lock menu item, the Keyboard hook is attached to the application and hooks the keyboard event. The UnHook CAPS Lock event removes installed hook from the application. Our example SDI contains two menu items called First Dialog and Second Dialog under view menu. These menu items will bring up two dialogs with text boxes in it. When a hook is installed, you can type only the capital letters in the dialogs. After opting for the Un-Hook action, you will not have upper case constraint on the dialogs. Let us jump into the explanation-based walk-through.