Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 17-Jan-13 10:57am
BBAzar410
Comments
Sergey Alexandrovich Kryukov at 17-Jan-13 17:00pm
   
System.Windows.Forms? Tag: "Forms".
—SA
Sergey Alexandrovich Kryukov at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
 
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:
 
public struct stMouseLoc
{
    public  int x;
    public int y;
    public int key;
}
 

put timer1.interval=1000; //1second
  Permalink  
v6
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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;
    }
}
  Permalink  

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



Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 17 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100