Monday, July 4, 2016

Handling Keyboard keys Using java code

import java.awt.Toolkit;
import java.awt.event.KeyEvent;

public class KeyLocksDemo {

public static void main(String[] args) {
KeyLocksDemo lc = new KeyLocksDemo();
lc.onAllKeys();
//              lc.offAllKeys();
     
 //this is an infinite loop that will make all 3 buttons contineous on-off
       /* int i = 0;
 // while(;;){SYSo();} --> Infinity loop
        while (i < 1) {
            lc.onAllKeys();
            lc.offAllKeys();
        }*/


}

public boolean capsLock(boolean cp) {
        Toolkit tk = Toolkit.getDefaultToolkit();
        tk.setLockingKeyState(KeyEvent.VK_CAPS_LOCK, cp);
        System.out.println("capsLock");
        return true;
       
    }
    public boolean numLock(boolean np) {
        Toolkit tk = Toolkit.getDefaultToolkit();
        tk.setLockingKeyState(KeyEvent.VK_NUM_LOCK, np);
        System.out.println("numLock");
        return true;
    }
    public boolean scrollLock(boolean sp) {
        Toolkit tk = Toolkit.getDefaultToolkit();
        tk.setLockingKeyState(KeyEvent.VK_SCROLL_LOCK, sp);
        System.out.println("scrollLock");
        return true;
    }
//This method on all three buttons
    public boolean onAllKeys() {
        return capsLock(true) & numLock(true) & scrollLock(true);
    }
    //This method off all three buttons
    public boolean offAllKeys() {
        return capsLock(false) & numLock(false) & scrollLock(false);
    }


}

No comments:

Post a Comment