Key Status StatusBar Demo
Description
Key Status StatusBar Demo demonstrates how to create a
StatusBar that displays indicators that indicate whether
your keyboard is in Caps Lock and/or Num Lock mode.

Key Status StatusBar Demo Screen Shot
Build the program before trying to display the main form (Form1)
in Design mode. Run the program and press the Caps Lock
and Num Lock keys and you'll see the CAP and NUM
indicators display as appropriate.
The C# concepts illustrated by this source code include:
- Using Platform Invoke to access Win32 APIs
- Creating multiple StatusBar panels
- Query the keyboard state by adding an OnIdle
event handler
It's only necessary to query the keyboard state when the
Application has emptied its message queue. KeyStatusStatusBarDemo
accomplishes this by adding an OnIdle event handler for its
status bar to the Application.Idle event handler:
public KeyStatusStatusBar(bool CapsLock, bool NumLock)
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
...
Application.Idle += new System.EventHandler(OnIdle);
}
...
[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true, CallingConvention=CallingConvention.Winapi)]
public static extern short GetKeyState(int keyCode);
public void OnIdle(object sender, EventArgs e)
{
// Update the panels when the program is idle.
bool CapsLock = (((ushort) GetKeyState(0x14 /*VK_CAPITAL*/)) & 0xffff) != 0;
bool NumLock = (((ushort) GetKeyState(0x90 /*VK_NUMLOCK*/)) & 0xffff) != 0;
if (CapsLockPanel != null)
{
CapsLockPanel.Text = CapsLock ? "CAP" : "";
}
if (NumLockPanel != null)
{
NumLockPanel.Text = NumLock ? "NUM" : "";
}
}
Requirements
This source code was developed with Microsoft Visual C# .net.
Source Code