Share via

What wrong is the coding?

Anonymous
2012-02-15T05:52:45+00:00

When I mark a stop sign, and check each step by press F8, and find that GetKeyState(SHIFT_KEY) and GetKeyState(Asc("H")) are still getting 1, even through I always release them at that moment.

Does anyone have any suggestions?

Thanks in advance for any suggestions

'Declare API

Declare Function GetKeyState Lib "User32" _

(ByVal vKey As Integer) As Integer

Const SHIFT_KEY = 16

Const CONTROL_KEY = 17

Function ShiftPressed() As Boolean

'Returns True if shift key is pressed

    Sheets("Date").Range("J1").Value = "" ' [Mark stop sign here]    Sheets("Date").Range("K1").Value = ""

    Sheets("Date").Range("L1").Value = ""

    ShiftPressed = GetKeyState(CONTROL_KEY) <> 1 Or GetKeyState(SHIFT_KEY) <> 1 'Or GetKeyState(Asc("H")) <> 1

    Sheets("Date").Range("M1").Value = ShiftPressed

    If GetKeyState(CONTROL_KEY) Then Sheets("Date").Range("J1").Value = GetKeyState(CONTROL_KEY)

    If GetKeyState(SHIFT_KEY) Then Sheets("Date").Range("K1").Value = GetKeyState(SHIFT_KEY)

    If GetKeyState(Asc("H")) Then Sheets("Date").Range("L1").Value = GetKeyState(Asc("H"))

End Function

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Andreas Killer 144.1K Reputation points Volunteer Moderator
2012-02-20T17:08:30+00:00

Try this one.

Andreas.

Option Explicit

Private Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal vKey As Long) As Long

Function ShiftPressed() As Boolean

  'True wenn die Shift-Taste gedrückt ist

  Const VK_SHIFT = &H10 ' Shift Taste

  ShiftPressed = GetAsyncKeyState(VK_SHIFT) And &H8000 <> 0

End Function

Function CtrlPressed() As Boolean

  'True wenn die Strg-Taste gedrückt ist

  Const VK_CONTROL = &H11 ' STRG Taste

  CtrlPressed = GetAsyncKeyState(VK_CONTROL) And &H8000 <> 0

End Function

Function AltPressed() As Boolean

  'True wenn die Alt-Taste gedrückt ist

  Const VK_MENU = &H12 ' Alt Taste

  AltPressed = GetAsyncKeyState(VK_MENU) And &H8000 <> 0

End Function

Function KeyPressed() As Boolean

  'True wenn eine Taste gedrückt ist

  Dim Cnt As Long

  For Cnt = 1 To 128

    If GetAsyncKeyState(Cnt) And &H8000 <> 0 Then

      KeyPressed = True

      Exit Function

    End If

  Next

End Function

Function ShiftWasPressed() As Boolean

  'True wenn die Shift-Taste gedrückt wurde (seit dem letzen Aufruf)

  Const VK_SHIFT = &H10 ' Shift Taste

  ShiftWasPressed = GetAsyncKeyState(VK_SHIFT) And 1 <> 0

End Function

Function CtrlWasPressed() As Boolean

  'True wenn die Strg-Taste gedrückt wurde (seit dem letzen Aufruf)

  Const VK_CONTROL = &H11 ' STRG Taste

  CtrlWasPressed = GetAsyncKeyState(VK_CONTROL) And 1 <> 0

End Function

Function AltWasPressed() As Boolean

  'True wenn die Alt-Taste gedrückt ist

  Const VK_MENU = &H12 ' Alt Taste

  AltWasPressed = GetAsyncKeyState(VK_MENU) And 1 <> 0

End Function

Function KeyWasPressed() As Boolean

  'True wenn eine Taste gedrückt ist

  Dim Cnt As Long

  For Cnt = 1 To 128

    If GetAsyncKeyState(Cnt) And 1 <> 0 Then

      KeyWasPressed = True

      Exit Function

    End If

  Next

End Function

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2012-02-21T16:04:11+00:00

    Thanks you very much for suggestions

    Was this answer helpful?

    0 comments No comments