Unresolve KeyDown verification when acceding a macro

mtheriault2000

Well-known Member
Joined
Oct 23, 2008
Messages
826
Hello again

An unresolve problem for me

A macro is activated when i press a button on my Excel sheet
I want to execute the macro only if at the same time the "ctrl" key is press. If not I will display " No action done"

I have work around Keydown but did not succeed

When i press the ctrl key while the mouse is over my macro box, The macro box is selected instead to execute the linked sub routine

This is needed as a security concept. I need the macro to be activated only if the CTRL key or another one is press

Any help appreciated
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Hello again

An unresolve problem for me

A macro is activated when i press a button on my Excel sheet
I want to execute the macro only if at the same time the "ctrl" key is press. If not I will display " No action done"

I have work around Keydown but did not succeed

When i press the ctrl key while the mouse is over my macro box, The macro box is selected instead to execute the linked sub routine

This is needed as a security concept. I need the macro to be activated only if the CTRL key or another one is press

Any help appreciated

Maybe this :

Code:
Private Declare Function GetAsyncKeyState Lib "user32" _
(ByVal vKey As Long) As Integer


Private Sub CommandButton1_Click()

    If Not CBool(GetAsyncKeyState(vbKeyControl)) Then GoTo Xit
    
    'Your button code goes here....
    MsgBox "hello!"


    Exit Sub
Xit:

    MsgBox "No Action done."
    
End Sub
 
Last edited:
Upvote 0
Hello Jaafar,
Thanks for your reply
Agains there to help me ... appreciated ;)

I did post a similar question about GetAsyncKeyState problem, few minutes before i saw your reply.

My function using GetAsyncKeyState work whell in debug mode ( step by step) but failed when in normal run

http://www.mrexcel.com/forum/showthread.php?t=559305&highlight=GetAsyncKeyState%28%26amp%3B

Note: I will use the shift key instead of the ctrl key. It interfere less in Excel process and anwer my need for the protection

My code
Code:
Private Declare Function GetAsyncKeyState Lib _
    "user32" (ByVal vKey As Long) As Integer

Public Sub TestShiftKey()
    If GetAsyncKeyState(&H10) Then
        ShiftKeyPress = True
        'MsgBox "Shift key is pressed"
    Else
        ShiftKeyPress = False
    End If
End Sub
 
Upvote 0
No, my code are only working in step by step mode
Will work on that this morning

Have a great day

Martin
Montreal Canada
 
Upvote 0
Resolve: Unresolve KeyDown verification when acceding a macro

Jaafar

Problem resolves. I added the Cbool function berore the GetAsynchKeyState command in my code.

I have never used Cbool before. I will read about it

Many thanks again
Code:
Public Sub TestShiftKey()
    If [COLOR="Red"]CBool[/COLOR](GetAsyncKeyState(&H10)) Then
        ShiftKeyPress = True
        MsgBox "Shift key is pressed"
    Else
        ShiftKeyPress = False
    End If
End Sub
 
Last edited:
Upvote 0
Jaafar

I'm using ActiveX button. I draw a rectangle over a region of my sheet, then applied a macro to it.

Less fancy than a true form, but so much less work for me since I'm not a programmer.

I'm creating an interface to read, validate , modify and send back orders info to a trading software, Multicharts.

Martin
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,836
Members
452,947
Latest member
Gerry_F

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