Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode <> KeyAsc Then
StartTime = Now()
KeyAsc = KeyCode
Debug.Print "S", StartTime
End If
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Dim PassTime As Long
Dim EndTime As Date
If KeyCode = KeyAsc Then
EndTime = Now()
Debug.Print "E", EndTime
PassTime = DateDiff("s", StartTime, EndTime)
MsgBox "按鍵經過" & PassTime & "秒"
End If
End Sub
uj5u.com熱心網友回復:
沒有復位? 同一按鍵只能檢測一次?
uj5u.com熱心網友回復:
Option Explicit
Dim StartTime As Date
Dim KeyAsc As Integer
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If (KeyCode <> KeyAsc) Or StartTime = 0 Then
StartTime = Now()
KeyAsc = KeyCode
Debug.Print "S", StartTime
End If
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Dim PassTime As Long
Dim EndTime As Date
If KeyCode = KeyAsc Then
EndTime = Now()
Debug.Print "E", EndTime
PassTime = DateDiff("s", StartTime, EndTime)
MsgBox "按鍵經過" & PassTime & "秒"
StartTime = 0
End If
End Sub
uj5u.com熱心網友回復:
第7行和19行的兩個判斷實際上就是一種配對以及復位的邏輯,你可以試試
uj5u.com熱心網友回復:
測驗了一下,的確有問題,修正一下
Dim StartTime As Date
Dim KeyAsc As Integer
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode <> KeyAsc Then
StartTime = Now()
KeyAsc = KeyCode
Debug.Print "S", KeyCode, StartTime
End If
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Dim PassTime As Long
Dim EndTime As Date
If KeyCode = KeyAsc Then
EndTime = Now()
Debug.Print "E", KeyCode, EndTime
PassTime = DateDiff("s", StartTime, EndTime)
KeyAsc = 0
Debug.Print "按鍵經過" & PassTime & "秒"
End If
End Sub
uj5u.com熱心網友回復:
測驗了一下,的確有問題,修正一下
Dim StartTime As Date
Dim KeyAsc As Integer
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode <> KeyAsc Then
StartTime = Now()
KeyAsc = KeyCode
Debug.Print "S", KeyCode, StartTime
End If
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Dim PassTime As Long
Dim EndTime As Date
If KeyCode = KeyAsc Then
EndTime = Now()
Debug.Print "E", KeyCode, EndTime
PassTime = DateDiff("s", StartTime, EndTime)
KeyAsc = 0
Debug.Print "按鍵經過" & PassTime & "秒"
End If
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode <> KeyAsc Then
StartTime = Now()
KeyAsc = KeyCode
Debug.Print "S", KeyCode, StartTime
End If
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Dim PassTime As Long
Dim EndTime As Date
If KeyCode = KeyAsc Then
EndTime = Now()
Debug.Print "E", KeyCode, EndTime
PassTime = DateDiff("s", StartTime, EndTime)
KeyAsc = 0
Debug.Print "按鍵經過" & PassTime & "秒"
End If
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode <> KeyAsc Then
StartTime = Now()
KeyAsc = KeyCode
Debug.Print "S", KeyCode, StartTime
End If
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Dim PassTime As Long
Dim EndTime As Date
If KeyCode = KeyAsc Then
EndTime = Now()
Debug.Print "E", KeyCode, EndTime
PassTime = DateDiff("s", StartTime, EndTime)
KeyAsc = 0
Debug.Print "按鍵經過" & PassTime & "秒"
End If
End Sub
就目前的方案和代碼,結果很明確,就是 A 鍵按下的時間。因為有效地記錄開始時間的是最后產生 KeyDown 事件的 KeyCode。而且只有相同 KeyCode 鍵的 KeyUp 事件結束計時。
uj5u.com熱心網友回復:
測驗了一下,的確有問題,修正一下
Dim StartTime As Date
Dim KeyAsc As Integer
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode <> KeyAsc Then
StartTime = Now()
KeyAsc = KeyCode
Debug.Print "S", KeyCode, StartTime
End If
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Dim PassTime As Long
Dim EndTime As Date
If KeyCode = KeyAsc Then
EndTime = Now()
Debug.Print "E", KeyCode, EndTime
PassTime = DateDiff("s", StartTime, EndTime)
KeyAsc = 0
Debug.Print "按鍵經過" & PassTime & "秒"
End If
End Sub