PC Review


Reply
Thread Tools Rate Thread

Haw can i test if CTRL is activated (hold down) in VBA

 
 
=?Utf-8?B?ZXhjZWxlbnQ=?=
Guest
Posts: n/a
 
      30th Jul 2006
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
 
Reply With Quote
 
 
 
 
Ron de Bruin
Guest
Posts: n/a
 
      30th Jul 2006
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" <(E-Mail Removed)> wrote in message news(E-Mail Removed)...
>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



 
Reply With Quote
 
 
 
 
Tom Ogilvy
Guest
Posts: n/a
 
      30th Jul 2006
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" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
>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



 
Reply With Quote
 
=?Utf-8?B?ZXhjZWxlbnQ=?=
Guest
Posts: n/a
 
      30th Jul 2006
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 ?

 
Reply With Quote
 
Tom Ogilvy
Guest
Posts: n/a
 
      30th Jul 2006
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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 ?
>



 
Reply With Quote
 
=?Utf-8?B?ZXhjZWxlbnQ=?=
Guest
Posts: n/a
 
      30th Jul 2006
ok tanks Tom now i think i got it

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I set ctrl+s and ctrl+o window size & ctrl+o save as type defa Moonbarker Osbourne Windows XP General 1 18th Jan 2008 03:10 PM
Hee Haw Hee Haw Sisteryaya Support 7 23rd Feb 2006 04:42 PM
Haw can I to create the numeric counter? frenic Microsoft Excel Programming 1 21st Jul 2005 11:12 AM
test test test test jadfl Microsoft Dot NET Framework 1 6th Jul 2005 06:31 AM
Haw can i print a document like in the "mirrow" style? =?Utf-8?B?VHpvbnk=?= Microsoft Word Document Management 1 6th Oct 2004 02:56 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:07 AM.