Detecting Keys Held down

G

Guest

Guest
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?
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,213,549
Messages
6,114,264
Members
448,558
Latest member
aivin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top