According to the keyboard, record keystrokes as long
CodePudding user response:
Keydown event has occurred in the record the start time
Keyup event recorded in the end of time
Then subtract
CodePudding user response:
Option Explicit Dim StartTime As Date Dim KeyAsc As Integer Private Sub Form_KeyDown (KeyCode As an Integer, Shift the As an Integer) If KeyCode & lt;> KeyAsc Then StartTime=Now () KeyAsc=KeyCode The Debug. Print "S", StartTime End the If End Sub Private Sub Form_KeyUp (KeyCode As an Integer, Shift the As an Integer) Dim PassTime As Long Dim EndTime As Date If KeyCode=KeyAsc Then The EndTime=Now () The Debug. Print "E", the EndTime PassTime=DateDiff (" s ", StartTime, EndTime) MsgBox "button after" & amp; PassTime & amp; "Seconds" End the If End Sub CodePudding user response:
refer to the second floor chewinggum response: Option Explicit Dim StartTime As Date Dim KeyAsc As Integer Private Sub Form_KeyDown (KeyCode As an Integer, Shift the As an Integer) If KeyCode & lt;> KeyAsc Then StartTime=Now () KeyAsc=KeyCode The Debug. Print "S", StartTime End the If End Sub Private Sub Form_KeyUp (KeyCode As an Integer, Shift the As an Integer) Dim PassTime As Long Dim EndTime As Date If KeyCode=KeyAsc Then The EndTime=Now () The Debug. Print "E", the EndTime PassTime=DateDiff (" s ", StartTime, EndTime) MsgBox "button after" & amp; PassTime & amp; "Seconds" End the If End Sub There is no reset? The same button can only detect one? CodePudding user response:
Option Explicit Dim StartTime As Date Dim KeyAsc As Integer Private Sub Form_KeyDown (KeyCode As an Integer, Shift the As an Integer) If (KeyCode & lt;> KeyAsc) Or StartTime=0 Then StartTime=Now () KeyAsc=KeyCode The Debug. Print "S", StartTime End the If End Sub Private Sub Form_KeyUp (KeyCode As an Integer, Shift the As an Integer) Dim PassTime As Long Dim EndTime As Date If KeyCode=KeyAsc Then The EndTime=Now () The Debug. Print "E", the EndTime PassTime=DateDiff (" s ", StartTime, EndTime) MsgBox "button after" & amp; PassTime & amp; "Seconds" StartTime=0 End the If End Sub CodePudding user response:
reference VBAHZ reply: 3/f Quote: refer to the second floor chewinggum response: Option Explicit Dim StartTime As Date Dim KeyAsc As Integer Private Sub Form_KeyDown (KeyCode As an Integer, Shift the As an Integer) If KeyCode & lt;> KeyAsc Then StartTime=Now () KeyAsc=KeyCode The Debug. Print "S", StartTime End the If End Sub Private Sub Form_KeyUp (KeyCode As an Integer, Shift the As an Integer) Dim PassTime As Long Dim EndTime As Date If KeyCode=KeyAsc Then The EndTime=Now () The Debug. Print "E", the EndTime PassTime=DateDiff (" s ", StartTime, EndTime) MsgBox "button after" & amp; PassTime & amp; "Seconds" End the If End Sub There is no reset? The same button can only detect one? Line 7 and line 19 two judgment is, in fact is a kind of matching and reset logic, you can try CodePudding user response:
Test and do have problems, changing the Dim StartTime As Date Dim KeyAsc As Integer Private Sub Form_KeyDown (KeyCode As an Integer, Shift the As an Integer) If KeyCode & lt;> KeyAsc Then StartTime=Now () KeyAsc=KeyCode The Debug. Print "S", KeyCode, StartTime End the If End Sub Private Sub Form_KeyUp (KeyCode As an Integer, Shift the As an Integer) Dim PassTime As Long Dim EndTime As Date If KeyCode=KeyAsc Then The EndTime=Now () The Debug. Print "E", KeyCode, EndTime PassTime=DateDiff (" s ", StartTime, EndTime) KeyAsc=0 The Debug. Print "button after" & amp; PassTime & amp; "Seconds" End the If End Sub CodePudding user response:
refer to 6th floor chewinggum response: test, do have problems, changing the Dim StartTime As Date Dim KeyAsc As Integer Private Sub Form_KeyDown (KeyCode As an Integer, Shift the As an Integer) If KeyCode & lt;> KeyAsc Then StartTime=Now () KeyAsc=KeyCode The Debug. Print "S", KeyCode, StartTime End the If End Sub Private Sub Form_KeyUp (KeyCode As an Integer, Shift the As an Integer) Dim PassTime As Long Dim EndTime As Date If KeyCode=KeyAsc Then The EndTime=Now () The Debug. Print "E", KeyCode, EndTime PassTime=DateDiff (" s ", StartTime, EndTime) KeyAsc=0 The Debug. Print "button after" & amp; PassTime & amp; "Seconds" End the If End Sub I think it still have problem, Such as press "key combination" Ctrl + A: Press the Ctrl key, don't let go, hold on just press the button A; And then let go of A button, release the Ctrl key nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull