Capture CTRL+C event in VBA

A

Andibevan

I am having problems being able to track when CTRL and C are pressed at the
same time. I have added the following to the keydown event of my Flexgrid
control:-

If KeyCode = vbKeyD And ((acCtrlMask) <> 0) Then
MsgBox "Ctrl + D Pressed"
End If
'
If KeyCode = vbKeyC And ((acCtrlMask) <> 0) Then
MsgBox "Ctrl + C Pressed"
End If

If I press CTRL+D I get the intended message but nothing if I press ctrl+c -
Any ideas how I can capture CTRL+c.

TIA

Andi
 
J

John Nurick

Hi Andi,

I just use the Keypress event and check for the ASCII values of the
control characters, e.g.

Private Sub Form_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 3
MsgBox "Ctrl-C"
Case 4
MsgBox "Ctrl-D"
Case Asc("C")
MsgBox "C"
End Select

As for the problem with your code, I haven't tried to think it through
completely, but the value of acCtrlMask is 2, so this
acCtrlMask <> 0
always evaluates to True.

If you want a condition that is true only when the Control key is
pressed, you need
(Shift And acCtrlMask) > 0

For more, see Help, or
http://msdn.microsoft.com/library/d...n-us/vbaac11/html/acevtKeyDown_HV03079790.asp
 
A

Andibevan

Hi John - Thanks for your suggestion but unfortunately it didn't work form
me.

This may because the flexgrid has the focus rather than the form. As a
result I moved your code to the Flexgrid keypress event and it still doesn't
work.

If I add the line debug.print keyascii - most combinations of keypresses
(i.e. ctrl+d, ctrl+r) work and generate an ascii value in the immediate
window. When I do ctrl+c, ctrl+v or ctrl+x - no ascii values are
generated - it seems as though the combination of ctrl+c triggers some event
elsewhere.

Still Stumped

Andi
 
J

John Nurick

I dunno. I've never used a Flexgrid, but Ctrl-C, Ctrl+X and Ctrl-V are
pretty suggestive: sounds like something is intercepting the clipboard
shortcuts before the keystroke is passed to the Flexgrid. Does the
active cell (or whatever it's called) of the Flexgrid have its own
keystroke events?

One thing that's worth trying is to set the Form's KeyPreview property
to True, and see if you can pick up these keystrokes in the Form's
keypress event. That should short-circuit any funny business with the
Flexgrid.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top