Error Checking in Excel
Thanks Thanks:  0
Likes Likes:  0
Results 1 to 3 of 3

Thread: Detecting Keys Held down

  1. #1
    Guest

    Default

    I want to use the IF procedure with a procedure for detecting whether the Control key (or Shift key) are being held down.
    If they are I want to exit the procedure otherwise I want to continue with my code. How can I detect these keys please?

  2. #2
    MrExcel MVP
    Join Date
    Feb 2002
    Location
    Sydney, Australia
    Posts
    2,940
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default

    One way you can do this is with API calls. How about this?

    Code:
    Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Long
    
    Const VK_SHIFT As Long = &H10
    Const VK_CONTROL As Long = &H11
    
    Sub YourProcedure()
    
    If GetKeyState(VK_SHIFT) < 0 Then
        MsgBox "Shift is being held"
    End If
    
    If GetKeyState(VK_CONTROL) < 0 Then
        MsgBox "Control is being held"
    End If
    
    End Sub
    HTH,
    D

  3. #3
    Guest

    Default

    Thanks. This works well enough in context.

    However, I am putting a file reference procedure (using the Bforeprint event) into the code. The problem is that I would like to save the procedure in a way that will enable any open workbook to use it.

    I want to save the beforeprint procedure (maybe in a class module) then save the workbook as an addin. I then want to be able to open any workbook and have the procedure apply to that workbook.

    Please please please can someone help me here! Thanks in advance.

    Jim

Some videos you may like

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •