4

When my application is opened, initially it can detect whether the numlock is on or off, but after I on or off the numlock when the application is still opened, the numlock status still remain same as the initial one. Here's my code:

bool isNumLockedPressed = System.Windows.Input.Keyboard.IsKeyToggled(System.Windows.Input.Key.NumLock);


public MainWindow()
{
            if (isNumLockedPressed == true)                
                NumLock.Foreground = System.Windows.Media.Brushes.White;

            else
                NumLock.Foreground = System.Windows.Media.Brushes.Red;     
}

I want the numlock status follow with my on/off action. Any suggestions?

UPDATED

This is my modification so far, but still can't get what I want. Any suggestions?

bool isNumLockedPressed = System.Windows.Input.Keyboard.IsKeyToggled(System.Windows.Input.Key.NumLock);

int numLockStatus { get; set; }

public MainWindow()
{
            if (isNumLockedPressed == true)
            {
                numLockStatus = 1;
                NumLock.Foreground = System.Windows.Media.Brushes.White;
            }

            else
            {
                numLockStatus = 0;
                NumLock.Foreground = System.Windows.Media.Brushes.Red;
            }

}   

 private void NumLockKey_Press(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.NumLock)
        {
            numLockStatus = 1;
            NumLock.Foreground = System.Windows.Media.Brushes.White;
        }

        else
        {
            numLockStatus = 0;
            NumLock.Foreground = System.Windows.Media.Brushes.Red;
        }
    }
YWah
  • 571
  • 13
  • 38
  • 1
    possible duplicate of [How can I find the state of NumLock, CapsLock and ScrollLock in .net?](http://stackoverflow.com/questions/577411/how-can-i-find-the-state-of-numlock-capslock-and-scrolllock-in-net) – horHAY Apr 21 '15 at 14:48
  • 2
    This is probably more than you want, but also have a look at "[*C# Actively detect Lock keys*](http://stackoverflow.com/q/17683620/1364007)". – Wai Ha Lee Apr 21 '15 at 14:57

4 Answers4

2

You are calling a function and storing the result in isNumLockedPressed. The value of isNumLockedPressed will never change unless you have code that is explicitly changing it. If you want to update it with the latest value later in your code, simply repeat the function call before you check the value isNumLockedPressed = System.Windows.Input.Keyboard.IsKeyToggled(System.Windows.Input.Key.NumLock);

Robert Levy
  • 28,747
  • 6
  • 62
  • 94
  • 3
    It seems to me he doesn't need help determining what the current NumLock value is before performing an action, he would like to be notified of when the NumLock value changes so he can update his GUI. – Derrick Moeller Apr 21 '15 at 15:01
2

So a bool is a value type, not a reference type. When you assign the value of IsKeyToggled to a value type, you are getting the value at that moment. If the value subsequently changes your local bool variable will not know it.

What you should do instead is create a utility/helper class with a static method/property.

public class KeyboardHelper 
{
    public bool IsNumLockPressed
    {
        get { return System.Windows.Input.Keyboard.IsKeyToggled(System.Windows.Input.Key.NumLock); }
    }
}
Richthofen
  • 2,076
  • 19
  • 39
1

Here's my solution:

bool isNumLockedPressed = System.Windows.Input.Keyboard.IsKeyToggled(System.Windows.Input.Key.NumLock);

int numLockStatus { get; set; }

public MainWindow()
{
            if (isNumLockedPressed == true)
            {
                numLockStatus = 1;
                NumLock.Foreground = System.Windows.Media.Brushes.White;
            }

            else
            {
                numLockStatus = 0;
                NumLock.Foreground = System.Windows.Media.Brushes.Red;
            }

}   

private void NumLockKey_Press(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.NumLock && e.IsToggled)
            {
                numLockStatus = 1;
                NumLock.Foreground = System.Windows.Media.Brushes.White;
            }

            else if (e.Key == Key.NumLock && e.IsToggled == false)
            {
                numLockStatus = 0;
                NumLock.Foreground = System.Windows.Media.Brushes.Red;
            }
        }
YWah
  • 571
  • 13
  • 38
0

If I understand your question right you want to get notified when the NumLock status changes.

To do that you you need a global keyboard hook. This way you get notified every time a key is pressed even when your application doesn't have keyboard focus. You can then react to it accrdingly in a callback.

Somebody posted a link to code here and as Wai Ha Lee mentioned there is an article here called "Low-Level Keyboard Hook in C#".

That being said, this is very intrusive and performance sensitive to create global hooks.

Community
  • 1
  • 1
Frank J
  • 1,666
  • 13
  • 19