|
-
Jul 3rd, 2012, 11:59 AM
#1
Thread Starter
Hyperactive Member
How to detect if keyboard idle and how long?
I've got an app that allows user to type in text and does an auto-search based on what the user typed. But if we do it after every key stroke, the early keystrokes lag because the search returns a lot.
So. Is there a way to determine how long ago the user last pressed a key?
-
Jul 3rd, 2012, 12:56 PM
#2
Re: How to detect if keyboard idle and how long?
Well if you take two(2) datetimes and add/subtract them, you will get a timespan. I think that's what you may be looking for, something like this:
Code:
Private dt As DateTime
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
dt = DateTime.Now
End Sub
Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
Dim ts As TimeSpan = DateTime.Now - dt
MessageBox.Show(ts.ToString, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
Mind you this would get annoying because for instance, me typing out quick reply, I've hit a key prob 100 times now, that would give me 100 + messagebox's. But it will getcha on the right path.
-
Jul 3rd, 2012, 01:12 PM
#3
Thread Starter
Hyperactive Member
Re: How to detect if keyboard idle and how long?
I know how to jerry-rig a solution, I was just wondering if one was built-in.
I COULD record the datetime of the last keystroke and the last search, then fire the search if the current datetime (from a timer tick event) is more than a second after the last keystroke AND no intervening search has fired.
I could do that, but I'm hoping for a simpler way.
-
Jul 3rd, 2012, 01:47 PM
#4
Re: How to detect if keyboard idle and how long?
I'm hoping for a simpler way
3 lines of code isn't simple enough? Any ways... As far as I know there is no built in function. But if you do use the route I suggested, don't use a timer, just compare the timespan. If the timespan's second is > 1(or w/e) then...
-
Jul 3rd, 2012, 02:07 PM
#5
Thread Starter
Hyperactive Member
Re: How to detect if keyboard idle and how long?
Have to use a timer to kick off the search... since the search is fired based upon INactivity, I have to have a timer fire to check for that inactivity.
-
Jul 3rd, 2012, 02:29 PM
#6
Re: How to detect if keyboard idle and how long?
You could use a timer, but it's not the only way. You could compare the timespan as shown above, or compare a variable based on the timer's tick. This is how it would look like if you used a timer:
Code:
Private i As Integer = 0
Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
Timer1.Stop()
MessageBox.Show("It has been " & i.ToString & " seconds since the a key was pressed.")
If i >= 0 Then
i = 0
Timer1.Start()
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
i += 1
End Sub
-
Jul 3rd, 2012, 02:47 PM
#7
Re: How to detect if keyboard idle and how long?
Here's an msdn article that talks about improving LINQ queries: http://msdn.microsoft.com/en-us/magazine/cc721610.aspx.
Whether you use LINQ or not you can benefit from the section that talks about performing incremental searches.
This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.
The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.
-
Jul 3rd, 2012, 04:38 PM
#8
Re: How to detect if keyboard idle and how long?
You might take a look at this link to give you other ideas about it. It's API calls, but might be more reliable:
http://dataerror.blogspot.com/2005/0...idle-time.html
My usual boring signature: Nothing
 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|