Search code examples
c#winformscapslocknum-lock

State of Num Caps ScrollLock in StatusStrip WindowsForms


Tell me, please, how do I show in StatusStrip when CAPS_LOCK key is enabled. I tried to follow the examples: one and two but nothing is displayed in my app. I created a new project, added StripStatusLabel element and tried to bring any information to it. It is strange that display is obtained only in the initialisation method:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        toolStripStatusLabel1.Text = "111";
    }
}

BUT in other method it's doesn't work.

using System.Diagnostics;
namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //toolStripStatusLabel1.Text = "111";
        }
        public void Form2_KeyDown(object sender, KeyEventArgs e)
        {
            Debug.Write("123");
            toolStripStatusLabel1.Text = "222";
        }
    }
}

Windows Forms. NetFramework 4.5 P.S. sorry for silly question :)


UPDATE: enter image description here

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            KeyDown += tst;
        }

        public void TextBoxTest()
        {
            textBox1.Text = "onetwo";
        }

        private void tst(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode & Keys.KeyCode) == Keys.CapsLock)
            {
                if (Control.IsKeyLocked(Keys.CapsLock))
                    toolStripStatusLabel1.Text = "Caps";
            }
        }
    }
}

But output don't work. Tell me please what I doing wrong


Solution

  • I solved it:

    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);
        capsStatusLabel.ForeColor = IsKeyLocked(Keys.CapsLock) ? statusStrip1.ForeColor : statusStrip1.BackColor;
        numStatusLabel.ForeColor = IsKeyLocked(Keys.NumLock) ? statusStrip1.ForeColor : statusStrip1.BackColor;
    }
    

    Thanks all!