Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 695
Default Haw can i test if CTRL is activated (hold down) in VBA

I wana use it in a event handle like

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
if not CTRL is pressed (hold down) then exit sub ???

haw do i test for that ?

tanks in advanse
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Haw can i test if CTRL is activated (hold down) in VBA

Hi excelent

This in a normal module

Declare Function GetKeyState Lib "user32" _
(ByVal nVirtKey As Long) As Integer

Const VK_CONTROL As Integer = &H11 'Ctrl


Sub test()
If GetKeyState(VK_CONTROL) < 0 Then Ctrl = True Else Ctrl = False
If Ctrl = True Then
MsgBox "pressed"
Else
MsgBox "Not"
End If
End Sub



And this in the sheet module

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Call test
End Sub




--
Regards Ron de Bruin
http://www.rondebruin.nl



"excelent" wrote in message ...
I wana use it in a event handle like

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
if not CTRL is pressed (hold down) then exit sub ???

haw do i test for that ?

tanks in advanse



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Haw can i test if CTRL is activated (hold down) in VBA

Here is some code to illustrate - it isn't how I would implement it, but
gives you the idea.

Declare Function GetKeyState Lib "User32" _
(ByVal vKey As Integer) As Integer

Const SHIFT_KEY = 16
Const CTRL_KEY = 17
Const ALT_KEY = 18


Sub find_key()


'Checks key states to see if both CTRL and ALT Keys are pressed
If GetKeyState(CTRL_KEY) < 0 And GetKeyState(ALT_KEY) < 0 Then
MsgBox "CTRL + ALT KEYS PRESSED"
'Checks key states to see if only CTRL key is pressed
ElseIf GetKeyState(CTRL_KEY) < 0 Then
MsgBox "CTRL KEY PRESSED"
'Checks key states to see if only ALT key is pressed
ElseIf GetKeyState(ALT_KEY) < 0 Then
MsgBox "ALT KEY PRESSED"
'Checks key states to see if only SHIFT key is pressed
ElseIf GetKeyState(SHIFT_KEY) < 0 Then
MsgBox "SHIFT KEY PRESSED"
End If


End Sub



--

Regards,

Tom Ogilvy




"excelent" wrote in message
...
I wana use it in a event handle like

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
if not CTRL is pressed (hold down) then exit sub ???

haw do i test for that ?

tanks in advanse



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 695
Default Haw can i test if CTRL is activated (hold down) in VBA

Great tanks Ron :-)

i wonder can i put this

If GetKeyState(VK_CONTROL) < 0 Then Ctrl = True Else Ctrl = False
If Ctrl = True Then
MsgBox "pressed"
Else
MsgBox "Not"
End If

in the Sheet event-module, or do i have to call it like u show it ?

and do i have to use the Declare Function too or is that just another
way to do the same ?

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Haw can i test if CTRL is activated (hold down) in VBA

Private Declare Function GetKeyState Lib "user32" _
(ByVal nVirtKey As Long) As Integer

Const VK_CONTROL As Integer = &H11 'Ctrl


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If GetKeyState(VK_CONTROL) < 0 Then Ctrl = True Else Ctrl = False
If Ctrl = True Then
MsgBox "pressed"
Else
MsgBox "Not"
End If

End Sub


all in the sheet module worked fine for me.

--
Regards,
Tom Ogilvy

"excelent" wrote in message
...
Great tanks Ron :-)

i wonder can i put this

If GetKeyState(VK_CONTROL) < 0 Then Ctrl = True Else Ctrl = False
If Ctrl = True Then
MsgBox "pressed"
Else
MsgBox "Not"
End If

in the Sheet event-module, or do i have to call it like u show it ?

and do i have to use the Declare Function too or is that just another
way to do the same ?





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 695
Default Haw can i test if CTRL is activated (hold down) in VBA

ok tanks Tom now i think i got it

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a difference between CTRL+D and CTRL+" (quotation marks) AKMMS Excel Discussion (Misc queries) 2 March 22nd 10 07:43 PM
How to make Ctrl-C, ctrl-V work in Office 2007 hj Excel Discussion (Misc queries) 1 June 23rd 09 01:09 PM
Excel 2007: Ctrl+PgUp or Ctrl+PgDn with Protected Sheets DrDave Excel Discussion (Misc queries) 1 July 28th 08 04:12 AM
Redefining the Ctrl [ and Ctrl ]s short cut with OnKey? John Wirt[_5_] Excel Programming 6 January 24th 05 05:46 AM
Disabling Ctrl-PgUp and Ctrl-PgDn K.A. Hueston Excel Programming 3 October 9th 04 12:31 AM


All times are GMT +1. The time now is 05:19 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"