locked
touchscreen activity RRS feed

  • Question

  • I have a vending application that uses a touchscreen, no keyboard or mouse. Is it possible in coding to sense if there has been no activity for a period of time to resort back to a home form?
    Monday, July 19, 2010 11:40 PM

Answers

  • You could write a lot of code to handle a bunch of events such as Click, etc. on your controls.  However, I think this will be most convenient for you if you periodically ask the system when the last input occurred.  The API function GetLastInputInfo performs this task.  This can be compared to the system current tick count (in milliseconds).  You can call out to it using DllImport (P/Invoke).

    Here is some demo code:

    Sub Main
    	Dim t As New TestForm
    	Application.Run(t)
    End Sub
    
    Class TestForm
    	Inherits Form
    	
    	Private m_Label As Label
    	Private m_Timer As System.Windows.Forms.Timer
    	
    	Public Sub New()
    		m_Label = new Label
    		Me.Controls.Add(m_Label)
    	
    		m_Timer = New System.Windows.Forms.Timer
    		m_Timer.Interval = 2000 'Check for activity this often.
    		m_Timer.Start()
    		AddHandler m_Timer.Tick, AddressOf m_Timer_Tick
    	End Sub
    	
    	Protected Overrides Sub OnFormClosed(ByVal e As FormClosedEventArgs)
    		m_Timer.Dispose()
    	End Sub
    	
    	Private Sub m_Timer_Tick(ByVal sender As Object, ByVal e As EventArgs)
    		Dim currentTick As UInteger = GetTickCount()
    		Dim pii As LASTINPUTINFO
    		pii.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(pii)
    		If Not GetLastInputInfo(pii) Then
    			Throw New System.ComponentModel.Win32Exception
    		End If
    		Dim lastInputTick As UInteger = pii.dwTime
    		Dim secondsAgo As Integer 
    		If currentTick >= lastInputTick Then
    			secondsAgo = (currentTick - lastInputTick) / 1000
    		Else
    			'Handle wraparound, very rare.
    			secondsAgo = (UInteger.MaxValue - lastInputTick + currentTick) / 1000
    		End If
    		
    		m_Label.Text = secondsAgo.ToString() + " seconds ago"
    		
    		'If secondsAgo >= YOUR_LIMIT Then
    			'Your code to go back to main form.
    		'End If
    	End Sub
    		
    	<System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)> _
    	Private Structure LASTINPUTINFO
    		Public cbSize As UInteger
    		Public dwTime As UInteger
    	End Structure
    	
    	<System.Runtime.InteropServices.DllImport("kernel32.dll")> _
    	Private Shared Function GetTickCount() As UInteger
    	End Function
    	
    	<System.Runtime.InteropServices.DllImport("user32.dll")> _
    	Private Shared Function GetLastInputInfo(ByRef pii As LastInputInfo) As <System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)> Boolean
    	End Function
    	
    End Class
    
    
    • Marked as answer by Liliane Teng Monday, July 26, 2010 11:30 AM
    Tuesday, July 20, 2010 12:59 AM

All replies

  • Use a timer to display the home form after a certain time.  Every time the touchscreen is used restart the timer.
    Tuesday, July 20, 2010 12:02 AM
  • i thought of that, but how do i start the timer on any touch?
    Tuesday, July 20, 2010 12:33 AM
  • You could write a lot of code to handle a bunch of events such as Click, etc. on your controls.  However, I think this will be most convenient for you if you periodically ask the system when the last input occurred.  The API function GetLastInputInfo performs this task.  This can be compared to the system current tick count (in milliseconds).  You can call out to it using DllImport (P/Invoke).

    Here is some demo code:

    Sub Main
    	Dim t As New TestForm
    	Application.Run(t)
    End Sub
    
    Class TestForm
    	Inherits Form
    	
    	Private m_Label As Label
    	Private m_Timer As System.Windows.Forms.Timer
    	
    	Public Sub New()
    		m_Label = new Label
    		Me.Controls.Add(m_Label)
    	
    		m_Timer = New System.Windows.Forms.Timer
    		m_Timer.Interval = 2000 'Check for activity this often.
    		m_Timer.Start()
    		AddHandler m_Timer.Tick, AddressOf m_Timer_Tick
    	End Sub
    	
    	Protected Overrides Sub OnFormClosed(ByVal e As FormClosedEventArgs)
    		m_Timer.Dispose()
    	End Sub
    	
    	Private Sub m_Timer_Tick(ByVal sender As Object, ByVal e As EventArgs)
    		Dim currentTick As UInteger = GetTickCount()
    		Dim pii As LASTINPUTINFO
    		pii.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(pii)
    		If Not GetLastInputInfo(pii) Then
    			Throw New System.ComponentModel.Win32Exception
    		End If
    		Dim lastInputTick As UInteger = pii.dwTime
    		Dim secondsAgo As Integer 
    		If currentTick >= lastInputTick Then
    			secondsAgo = (currentTick - lastInputTick) / 1000
    		Else
    			'Handle wraparound, very rare.
    			secondsAgo = (UInteger.MaxValue - lastInputTick + currentTick) / 1000
    		End If
    		
    		m_Label.Text = secondsAgo.ToString() + " seconds ago"
    		
    		'If secondsAgo >= YOUR_LIMIT Then
    			'Your code to go back to main form.
    		'End If
    	End Sub
    		
    	<System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)> _
    	Private Structure LASTINPUTINFO
    		Public cbSize As UInteger
    		Public dwTime As UInteger
    	End Structure
    	
    	<System.Runtime.InteropServices.DllImport("kernel32.dll")> _
    	Private Shared Function GetTickCount() As UInteger
    	End Function
    	
    	<System.Runtime.InteropServices.DllImport("user32.dll")> _
    	Private Shared Function GetLastInputInfo(ByRef pii As LastInputInfo) As <System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)> Boolean
    	End Function
    	
    End Class
    
    
    • Marked as answer by Liliane Teng Monday, July 26, 2010 11:30 AM
    Tuesday, July 20, 2010 12:59 AM