0

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

Community
  • 1
  • 1
Fundo
  • 1
  • 5
  • 1
    Using the form's KeyDown event isn't good enough, it can only fire when the form doesn't have any controls that can get the focus and thus get the KeyDown event. [Read this](http://stackoverflow.com/a/400325/17034). – Hans Passant Mar 15 '16 at 08:29
  • Since the `CapsLock` can be turn on or turn off when your application doesn't have focus, as a simple and reliable solution you can check `CapsLock` status in `Application.Idle` event. Handling keyboard events of the form is not enough to detect changes on the key lock state and you should also put your logic in some other places like activation event of your form or you need to register a global keyboard hook. – Reza Aghaei Mar 15 '16 at 13:30

2 Answers2

0

// Caps Lock

toolStripStatusLabel1.Text=IsKeyLocked(Keys.CapsLock).toString();  

// Num Lock

toolStripStatusLabel1.Text=IsKeyLocked(Keys.NumLock).toString();

Set KeyPreview property of your form to set true write this code is key_down event of your form

Before this you can't put text in function other than IntializeComponent because KeyPreview property for your form is set to false make sure it is sure to make key down event working

Hassan Tariq
  • 730
  • 7
  • 15
-1

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!

Fundo
  • 1
  • 5