35
https://www.facebook.com/Oxus20 [email protected] JAVA Virtual Keyboard » Exception Handling » JToggleButton Class » Robot Class » Toolkit Class Prepared By: Nahid Ahmadi Edited By: Abdul Rahman Sherzad

Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes

Embed Size (px)

DESCRIPTION

A Virtual Keyboard is considered to be a component to use on computers without a real keyboard e.g. Touch Screen Computers and Smart Phones; where a mouse can utilize the keyboard functionalities and features. In addition, Virtual Keyboard used for the following subjects: Foreign Character Sets, Touchscreen, Bypass Key Loggers, etc.

Citation preview

Page 1: Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes

https://www.facebook.com/Oxus20

[email protected] JAVA Virtual

Keyboard

» Exception

Handling

» JToggleButton Class

» Robot Class

» Toolkit Class

Prepared By: Nahid Ahmadi

Edited By: Abdul Rahman Sherzad

Page 2: Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes

Agenda

» Virtual Keyboard

» Exception Handling

» JToggleButton Class

» Robot Class

» Toolkit Class

» Virtual Keyboard Implementation

2

https://www.facebook.com/Oxus20

Page 3: Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes

https://www.facebook.com/Oxus20

3

Page 4: Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes

Virtual Keyboard Using JAVA

4

https://www.facebook.com/Oxus20

Page 5: Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes

Introduction to Virtual Keyboard

» A virtual keyboard is considered to be a

component to use on computers without a

real keyboard e.g.

˃ touch screen computer

» A mouse can utilize the keyboard

functionalities and features. 5

https://www.facebook.com/Oxus20

Page 6: Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes

Virtual Keyboard Usage

» It is possible to obtain keyboards for the following specific purposes: ˃ Keyboards to fill specific forms on sites

˃ Special key arrangements

˃ Keyboards for dedicated commercial sites

˃ etc.

» In addition, Virtual Keyboard used for the following subjects: ˃ Foreign Character Sets

˃ Touchscreens

˃ Bypass key loggers

6

https://www.facebook.com/Oxus20

Page 7: Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes

https://www.facebook.com/Oxus20

7

Page 8: Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes

Exception Handling » A program can be written assuming that nothing unusual or

incorrect will happen.

˃ The user will always enter an integer when prompted to do so.

˃ There will always be a nonempty list for a program that takes an entry from the list.

˃ The file containing the needed information will always exist.

» Unfortunately, it isn't always so.

» Once the core program is written for the usual, expected

case(s), Java's exception-handling facilities should be

added to accommodate the unusual, unexpected case(s). 8

Page 9: Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes

Exception Hierarchy

9

https://www.facebook.com/Oxus20

Page 10: Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes

Exception Handling Demo Source code

public class ExceptionHandlingDemo {

public static void main(String args[]) {

try {

int scores[] = { 90, 85, 75, 100 };

System.out.println("Access element nine:" + scores[9]);

} catch (ArrayIndexOutOfBoundsException e) {

System.out.println("Exception thrown:" + e);

}

System.out.println("\nWithout Exception Handling I was not able to execute and print!");

}

}

10

https://www.facebook.com/Oxus20

Page 11: Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes

Exception Handling Demo OUTPUT

Exception

thrown:java.lang.ArrayIndexOutOfBoundsEx

ception: 9

Without Exception Handling I was not

able to execute and print!

11

https://www.facebook.com/Oxus20

Page 12: Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes

https://www.facebook.com/Oxus20

12

Page 13: Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes

JToggleButton Class

» JToggleButton is an implementation of a two-state

button and is used to represent buttons that can be

toggled ON and OFF

» The JRadioButton and JCheckBox classes are

subclasses of this class.

» The events fired by JToggleButtons are slightly

different than those fired by JButton. 13

https://www.facebook.com/Oxus20

Page 14: Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes

JToggleButton Demo

» The following example on next slide demonstrates

a toggle button. Each time the toggle button is pressed,

its state is displayed in a label.

» Creating JToggleButton involves these steps:

1. Create an instance of JToggleButton.

2. Register an ItemListener to handle item events generated by the button.

3. To determine if the button is on or off, call isSelected().

14

https://www.facebook.com/Oxus20

Page 15: Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes

JToggleButton Demo Source Code import java.awt.FlowLayout;

import java.awt.event.ItemEvent;

import java.awt.event.ItemListener;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JToggleButton;

class JToggleButtonDemo {

JLabel lblOutput;

JToggleButton btnOnOff;

JFrame win;

JToggleButtonDemo() {

// JFrame Customization

win = new JFrame("Using JToggleButton");

win.setLayout(new FlowLayout());

win.setSize(300, 80);

win.setLocationRelativeTo(null);

win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 15

https://www.facebook.com/Oxus20

Page 16: Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes

// JToggleButton and JLabel Customization

lblOutput = new JLabel("State : OFF");

btnOnOff = new JToggleButton("On / Off", false);

// Add item listener for JToggleButton.

btnOnOff.addItemListener(new ItemListener() {

public void itemStateChanged(ItemEvent ie) {

if (btnOnOff.isSelected()) {

lblOutput.setText("State : ON");

} else {

lblOutput.setText("State : OFF");

}

}

});

// Add toggle button and label to the content pane.

win.add(btnOnOff);

win.add(lblOutput);

win.setVisible(true);

}

public static void main(String args[]) {

new JToggleButtonDemo();

}

} 16

https://www.facebook.com/Oxus20

Page 17: Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes

JToggleButton Demo OUTPUT

17

https://www.facebook.com/Oxus20

Page 18: Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes

https://www.facebook.com/Oxus20

18

Page 19: Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes

Java Robot Class

» This class is used to generate native system input events for the purposes of ˃ test automation

˃ self-running demos

˃ and other applications where control of the mouse and keyboard is needed.

» This class has three main functionalities: ˃ mouse control

˃ keyboard control

˃ and screen capture.

https://www.facebook.com/Oxus20

19

Page 20: Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes

Robot Class Demo

» Perform keyboard operation with help of java Robot class.

» The following example on next slide will demonstrate the

Robot class usage to handle the keyboard events.

» The program will write the word "OXUS20" inside the

TextArea automatically after running the program.

» The word "OXUS20" will be written character by character

with one second delay between each on of the

characters. 20

https://www.facebook.com/Oxus20

Page 21: Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes

Robot Class Demo Source Code import java.awt.AWTException; import java.awt.BorderLayout; import java.awt.Robot; import java.awt.event.KeyEvent; import javax.swing.JFrame; import javax.swing.JTextArea; public class RobotDemo extends JFrame { public RobotDemo() { // JFrame with TextArea Settings setTitle("OXUS20 Robot Demo"); setSize(400, 200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); JTextArea txtOXUS = new JTextArea(); add(txtOXUS, BorderLayout.CENTER); setVisible(true); }

21

https://www.facebook.com/Oxus20

Page 22: Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes

public static void main(String[] args) {

new RobotDemo();

// Store Keystrokes "OXUS20" in an array

int keyInput[] = { KeyEvent.VK_O, KeyEvent.VK_X, KeyEvent.VK_U,

KeyEvent.VK_S, KeyEvent.VK_2, KeyEvent.VK_0 };

try {

Robot robot = new Robot();

// press the shift key

robot.keyPress(KeyEvent.VK_SHIFT);

// This types the word 'OXUS20' in the TextArea

for (int i = 0; i < keyInput.length; i++) {

if (i > 0) {

robot.keyRelease(KeyEvent.VK_SHIFT);

}

robot.keyPress(keyInput[i]);

// pause typing for one second

robot.delay(1000);

}

} catch (AWTException e) {

System.err.println("Exception is happening!");

}

} // end of main()

} // end of RobotDemo class 22

https://www.facebook.com/Oxus20

Page 23: Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes

https://www.facebook.com/Oxus20

23

Page 24: Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes

Toolkit class » Toolkit is an AWT class acting as a base class for all

implementations of AWT.

» This class offers a static method getDefaultToolkit() to return

a Toolkit object representing the default implementation of

AWT.

» You can use this default toolkit object to get information of the

default graphics device, the local screen, and other purposes.

» Next slide demonstrate an example finding out the actual size

and resolution of your screen.

24

https://www.facebook.com/Oxus20

Page 25: Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes

Toolkit Class Demo Source Code

import java.awt.Dimension;

import java.awt.Toolkit;

public class ToolkitDemo {

public static void main(String[] a) {

Toolkit tk = Toolkit.getDefaultToolkit();

Dimension d = tk.getScreenSize();

System.out.println("Screen size: " + d.width + "x" + d.height);

System.out.println("\nScreen Resolution: " + tk.getScreenResolution());

}

} 25

https://www.facebook.com/Oxus20

Page 26: Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes

Toolkit Class Demo OUTPUT

Screen Size: 1366x768

Screen Resolution: 96

26

https://www.facebook.com/Oxus20

Page 27: Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes

Another Toolkit Example » Change the state of the Caps Lock key

˃ If Caps Lock key is ON turn it OFF

˃ Otherwise, if Caps Lock key is OFF turn it ON

» Toolkit.getDefaultToolkit().setLockingKeyState( KeyEvent.VK_CAPS_LOCK, false); ˃ This line of code will change the state of the Caps Lock key OFF.

» Toolkit.getDefaultToolkit().setLockingKeyState( KeyEvent.VK_CAPS_LOCK, true); ˃ This line of code will change the state of the Caps Lock key ON.

» Accordingly the keyboard LEDs flash will become

ON or OFF. 27

https://www.facebook.com/Oxus20

Page 28: Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes

Another Toolkit Class Demo Source Code import java.awt.FlowLayout;

import java.awt.Toolkit;

import java.awt.event.ItemEvent;

import java.awt.event.ItemListener;

import java.awt.event.KeyEvent;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JToggleButton;

class ChangeCapsLockStateDemo {

JLabel lblOutput;

JToggleButton btnOnOff;

JFrame win;

ChangeCapsLockStateDemo() {

// JFrame Customization

win = new JFrame("Using JToggleButton");

win.setLayout(new FlowLayout());

win.setSize(300, 80);

win.setLocationRelativeTo(null);

win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 28

https://www.facebook.com/Oxus20

Page 29: Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes

// JToggleButton and JLabel Customization

lblOutput = new JLabel("CapsLock : OFF");

Toolkit.getDefaultToolkit().setLockingKeyState(KeyEvent.VK_CAPS_LOCK, false);

btnOnOff = new JToggleButton("On / Off", false);

// Add item listener for JToggleButton.

btnOnOff.addItemListener(new ItemListener() {

public void itemStateChanged(ItemEvent ie) {

if (btnOnOff.isSelected()) {

lblOutput.setText("CapsLock : ON");

Toolkit.getDefaultToolkit().setLockingKeyState(KeyEvent.VK_CAPS_LOCK, true);

} else {

lblOutput.setText("CapsLock : OFF");

Toolkit.getDefaultToolkit().setLockingKeyState(KeyEvent.VK_CAPS_LOCK, false);

}

}

});

// Add toggle button and label to the content pane.

win.add(btnOnOff);

win.add(lblOutput);

win.setVisible(true);

}

public static void main(String args[]) {

new ChangeCapsLockStateDemo();

}

} 29

https://www.facebook.com/Oxus20

Page 30: Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes

Another Toolkit Class Demo OUPUT

30

https://www.facebook.com/Oxus20

Page 31: Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes

https://www.facebook.com/Oxus20

31

Page 32: Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes

Java Virtual Keyboard Source Code import java.awt.AWTException; import java.awt.Color; import java.awt.Robot; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.event.KeyEvent; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JToggleButton; public class JavaVirtualKeyboard implements ActionListener, ItemListener {

32

https://www.facebook.com/Oxus20

Page 33: Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes

Java Virtual Keyboard OUTPUT

33

https://www.facebook.com/Oxus20

Page 34: Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes

Complete Source Code will be published very soon

Check Our Facebook Page

(https://www.facebook.com/Oxus20)

34

https://www.facebook.com/Oxus20

Page 35: Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes

END

https://www.facebook.com/Oxus20

35