Detect Application Idle - KeyDown not functioning
Hi all,
I am implementing an idle function whereby if the application has no input for a few seconds, it will refresh the DataGridView table.
To implement, if the app is idle, an IdleTimer will start.
If the user moves mouse or press a key, the IdleTimer will stop, hence resetting the timeout.
Application_Idle works
Code:
Private Sub Application_Idle(ByVal sender As Object, ByVal e As EventArgs)
IdleTimer.Start()
End Sub
MouseMove works
Code:
Private Sub adminViewForm_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
MessageBox.Show("MouseMove")
IdleTimer.Stop()
End Sub
KeyDown does not work. The MessageBox does not appear.
Code:
Private Sub adminViewForm_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
MessageBox.Show("KeyPress")
IdleTimer.Stop()
End Sub
Refresh Table
Code:
Private Sub IdleTimer_Tick(sender As Object, e As EventArgs) Handles IdleTimer.Tick
EVC_ProjectsDataSet.Clear()
ProjectsTableAdapter.Fill(EVC_ProjectsDataSet.Projects)
save_btn.BackColor = DefaultBackColor
AddNew_btn.Enabled = True
End Sub
Any advise is appreciated.
Re: Detect Application Idle - KeyDown not functioning
By default the form will not receive keyboard events if a control has focus.
To enable it, set the KeyPreview property of the form to True.
Re: Detect Application Idle - KeyDown not functioning
Quote:
Originally Posted by
si_the_geek
By default the form will not receive keyboard events if a control has focus.
To enable it, set the KeyPreview property of the form to True.
How do I do that?
I added KeyPreview but nothing happened.
Is this the correct way?
Code:
Private Sub adminViewForm_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
Me.KeyPreview = True
MessageBox.Show("KeyPress")
IdleTimer.Stop()
End Sub
Re: Detect Application Idle - KeyDown not functioning
it is a design time property of the form... you set it at design time so that it is set BEFORE your event... if you don't change the keypreview until after the event fires... but you need the property set before the event can fire.... not only have you put the cart before the horse, but the cart is in the next town ahead.
-tg
Re: Detect Application Idle - KeyDown not functioning
So in short, I should add in Form_Load.
Got it.
Re: Detect Application Idle - KeyDown not functioning
you could... I wouldn't... I'd set it at design time...
-tg
Re: Detect Application Idle - KeyDown not functioning
Quote:
Originally Posted by
techgnome
you could... I wouldn't... I'd set it at design time...
-tg
Please elaborate what you mean by Design Time.
Re: Detect Application Idle - KeyDown not functioning
When you are creating forms visually in Visual Studio by adding your form and dragging and dropping your controls e.t.c thats your design time or design mode.
Select your form, go to the properties window, look for the property KeyPreview and change it from False to True
Re: Detect Application Idle - KeyDown not functioning
you know.. at design time... when you're designing the form.. .as in not run-time, which is when your app is running.
when you have the IDE open and the app isn't running, that's "design time" mode... where you "design" the forms and what nots.
-tg
Re: Detect Application Idle - KeyDown not functioning
I see.
Thanks. I didn't notice that the property was available.