Keylock Monitor

A keyboard locking indicator

Page 1 of 1

2 Replies - 1756 Views - Last Post: 17 January 2009 - 07:09 AM Rate Topic: -----

#1 TheGuy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-January 09

Keylock Monitor

Posted 17 January 2009 - 04:35 AM

Hey everyone.
Sorry for the lack of creativity when choosing my username, it was the first that worked...

On to what brought me here: My laptop's keyboard doesn't have the 3 Numlock, CapsLock and ScrollLock lights. After searching around for a program to do that, I decided to make my own. And I did:
import java.awt.Toolkit;
import java.awt.event.KeyEvent;

public class NumCapScroll {
	public static boolean isNumLockSet() {
		return Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_NUM_LOCK);
	}

	public static boolean isCapsLockSet() {
		return Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK);
	}

	public static boolean isScrollLockSet() {
		return Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_SCROLL_LOCK);
	}
}


And then I made a panel to show this info:
public class Panel1 extends JPanel {
	private JCheckBox cbNum = new JCheckBox();
	private JCheckBox cbCaps = new JCheckBox();
	private JCheckBox cbScroll = new JCheckBox();
	
	Timer t = new Timer(500, new ActionListener() {
		public void actionPerformed(ActionEvent e) { refresh(); }
	} );

	private void init() {
		// position everything
		t.start();
	}

	public void refresh() {
		cbNum.setSelected(NumCapScroll.isNumLockSet());
		cbCaps.setSelected(NumCapScroll.isCapsLockSet());
		cbScroll.setSelected(NumCapScroll.isScrollLockSet());
	}
}


Everything works alright ( with some acceptable lag ) and I even found a way to create a system tray icon with this information, but I was wondering if there is a better way to do this. Like listening on system-wide keyboard events or something, because using a timer for this doesn't feel quite right.

Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: Keylock Monitor

#2 ayman_mastermind  Icon User is offline

  • human.setType("geek");
  • member icon

Reputation: 126
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: Keylock Monitor

Posted 17 January 2009 - 05:21 AM

I would like first to welcome you D.I.C. , and tell you that your program's idea is cool and useful... for adding a system tray icon, check out this link it shows you how to do that and keep your program running in the backgound:
http://java.sun.com/...systemtray.html i hope this helps, good luck in your program and i hope that you will have a good and rewarding time here ;)
Was This Post Helpful? 0
  • +
  • -

#3 TheGuy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-January 09

Re: Keylock Monitor

Posted 17 January 2009 - 07:09 AM

Thank you ayman_mastermind and I'm glad you liked my program.
However, as you can read from the end of my post, I already knew how to go about using the system tray ( although I was yet to find it in the java tutorial, thanks for that ).

My problem really lies with the use of the Timer for this particular situation, as I feel it is not the best way to do it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1