Search code examples
c#winformsuser-interfacetouchpixelsense

C# Win Form: Detect last interaction (touch) and display confirmation box


Windows Form Application:

Is there a way to check the time for which the device (Microsoft Surface 55 inch touchscreen) hasn't received any interaction (touch) from the user and if it is more than 5 mins than a confirmation box should be displayed asking whether you want to continue or exit. For this 5 mins either a flash file or a video will be running. If the video is running (I have embedded AxWindowsMediaPlayer in my Win Form) then it should be paused and then the confirmation box should be displayed.

I want to achieve this using C# code on Visual Studio 2013 Professional.

I came across a few functions and threads:

One of the option i found is to use GetLastInputInfo() function. However, I am not sure whether it will work for touch interactivity. I don't have a Microsoft Surface device to test. A quote from MSDN:

This function is useful for input idle detection. However, GetLastInputInfo does not provide system-wide user input information across all running sessions. Rather, GetLastInputInfo provides session-specific user input information for only the session that invoked the function. The tick count when the last input event was received (see LASTINPUTINFO) is not guaranteed to be incremental. In some cases, the value might be less than the tick count of a prior event. For example, this can be caused by a timing gap between the raw input thread and the desktop thread or an event raised by SendInput, which supplies its own tick count.

I also came across Application.idle event

Anyone has a workaround or useful links for this will be highly appreciated.


Solution

  • This piece of code did the trick for me:

    private void timer1_Tick(object sender, EventArgs e)
        {           
            tagLASTINPUTINFO LastInput = new tagLASTINPUTINFO();
            Int32 IdleTime;
            LastInput.cbSize = (uint)Marshal.SizeOf(LastInput);
            LastInput.dwTime = 0;
    
            if (GetLastInputInfo(ref LastInput))
            {
                IdleTime = System.Environment.TickCount - LastInput.dwTime;
                if (IdleTime > 10000)
                {
                    axWindowsMediaPlayer1.Ctlcontrols.pause();
                    timer1.Stop();
                    string timeRemaining = IdleTime.ToString();
                    lbl_TimeRemaining.Text = IdleTime.ToString();
                    lbl_TimeRemaining.Show();
                    MessageBox.Show("Do you wish to continue?");
                    lbl_TimeRemaining.Hide();
                }
                else
                {
    
                }
                timer1.Start();
                axWindowsMediaPlayer1.Ctlcontrols.play();
            }            
            string title = axWindowsMediaPlayer1.currentMedia.getItemInfo("Title");
        }
    

    For the first time you will have to start the timer by calling timer1.start(); from inside the FormLoad() method