Click here to Skip to main content
15,039,149 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please, is there a way to find out from an opened form, when a key was pressed or when a mouse was moved.
I am designing a C# application which for one form, I would like to lock the controls of the form by disabling them if after some time the user does not press any key or move the mouse.
Please folks, I'm short of ideas. Your helping hand will be gratefully appreciated. Thanks.
Posted
Comments
Sergey Alexandrovich Kryukov 17-Jan-13 17:00pm
   
System.Windows.Forms? Tag: "Forms".
—SA
Sergey Alexandrovich Kryukov 17-Jan-13 17:01pm
   
First of all: why are you so hostile to your users? Or is it some kind of programming humor?
What's the problem? you have all the mouse and keyboard events to handle, you can get current time. What could be a problem?
—SA

it is very easy , use for each for all of the controls in your form and put the enable=false. use a timer in your program that stores mouse location.and compare 7 state of your mouse in a time range like 20 seconds and if the state is not changed make all of the enable of all of controls to false. need more help? comment.

C#
int Current_Key=0;
      stMouseLoc[] mouseloc = new stMouseLoc[20];
      int mouse_Currentposition_X;
      int mouse_Currentposition_Y;
      private void timer1_Tick(object sender, EventArgs e)
      {

          stMouseLoc currentmouseloc = new stMouseLoc();
          currentmouseloc.x = mouse_Currentposition_X;
          currentmouseloc.y = mouse_Currentposition_Y;
          currentmouseloc.key = Current_Key;
          mouseloc[Current_Key] = currentmouseloc;


          Current_Key++;
          bool movement = false;
          if (Current_Key == 20)
          {
              for (int i = 1; i < 20; i++)
              {
                  if (mouseloc[0].x != mouseloc[i].x || mouseloc[0].x != mouseloc[i].x)
                  {
                      movement = true;
                  }
              }
              if (!movement) fncFreeze();
              Current_Key = 0;
          }
      }
      public void fncFreeze()
      {
          foreach (Control X in this.Controls)
          {
              X.Enabled= false;
          }
      }
      private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            mouse_Currentposition_X = e.X;
            mouse_Currentposition_Y = e.Y;
        }


use array of a struct like this:

SQL
public struct stMouseLoc
{
    public  int x;
    public int y;
    public int key;
}



put timer1.interval=1000; //1second
   
v6
C#
public partial class Form1 : Form
{
    Timer timer1 = new Timer();
    public Form1()
    {
        InitializeComponent();
        timer1.Interval = 3000;
        timer1.Tick += new EventHandler(timer1_Tick);
        this.MouseMove += new MouseEventHandler(Form1_MouseMove);
        this.KeyPress += new KeyPressEventHandler(Form1_KeyPress);
        button1.MouseMove += new MouseEventHandler(Form1_MouseMove);
        button1.KeyPress += new KeyPressEventHandler(Form1_KeyPress);
    }

    void Form1_KeyPress(object sender, KeyPressEventArgs e)
    {
        timer1.Stop();
        timer1.Start();
    }

    void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        timer1.Stop();
        timer1.Start();
    }

    void timer1_Tick(object sender, EventArgs e)
    {
        timer1.Stop();
        button1.Enabled = false;
    }
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900