Log inUsernamePassword
Log me on automatically each visit    
Register
Register
Log in to check your private messages
Log in to check your private messages
Visual Basic Forum for Visual Basic Programmers VB Forum Index » Knowledge Base

Post new topic   Reply to topic
Be careful testing for Control, Alt, and Shift keys in VB6
View previous topic :: View next topic  
Author Message
DoobieKeebler
Moderator


Joined: 17 Jun 2005
Posts: 254
Location: 181°15'2.003"W, 93°5'16.956"N

PostPosted: Jun 17th, 2005 02:05 PM    Post subject: Be careful testing for Control, Alt, and Shift keys in VB6 Reply with quote

I'm writing a WordPad-type editor and I want to allow keyboard shortcuts for things like Bold and Italics. So I checked out the MSDN Library and copied the example code from the KeyUp topic. I went to test it and got a nasty surprise.

I tried pressing Ctrl+B and my code worked fine. Then I tried Ctrl+Alt+B and my Ctrl+B code fired. After testing a few more key combinations I found that as long as Ctrl was pressed, my code fired. Hmmm. After spending a few minutes studying the code, it hit me. The code checks if the Ctrl key is pressed, but it doesn't check if it's the only one of the three modifier keys pressed.

I added a few more lines of code and got it to work like I though it would work originally. You just have to test for the modifier keys you do and don't want pressed. The original code is in red. My contributions are in green.

Private Sub Text1_KeyDown (KeyCode As Integer, Shift As Integer
Dim ShiftDown, AltDown, CtrlDown

Dim CtrlOnlyDown, AltOnlyDown, ShiftOnlyDown
Dim CtrlShiftOnlyDown, CtrlAltOnlyDown, AltShiftOnlyDown
Dim CtrlAltShiftDown


ShiftlDowm = (Shift And vbShiftMask) > 0
AltDown = (Shift And vbAltMask) > 0
CtrlDown = (Shift And vbCtrlMask) > 0


CtrlOnlyDown = (CtrlDown And (Not AltDown) And (Not ShiftDown))
AltOnlyDown = ((Not CtrlDown) And AltDown And (Not ShiftDown))
ShiftOnlyDown = ((Not CtrlDown) And (Not AltDown) And ShiftDown)
CtrlAltOnlyDown = (CtrlDown And AltDown And (Not ShiftDown))
CtrlShiftOnlyDown = (CtrlDown And (Not AltDown) And (ShiftDown)
AltShiftOnlyDown = ((Not CtrlDown) And AltDown And ShiftDown)
CtrlAltShiftDown = (CtrlDown And AltDown And ShiftDown)

' Test for the key combinations
If CtrlOnlyDown And (KeyCode = vbKeyB) Then
' Fire Ctrl+B code (normally Bold)
End If

If CtrlShiftOnlyDown And (KeyCode = vbKeyS) Then
' Fire Ctrl+Shift+S code (normally Save As)
End If

End Sub

Hopefully this will make somebody's life a little easier now that you don't have to test for modifier key combos in a particular order.
Back to top
View user's profile Send private message
Under--Score
Regular


Joined: 19 Jul 2005
Posts: 88
Location: Location Location

PostPosted: Sep 15th, 2005 01:02 PM    Post subject: Reply with quote

I'm sorry but what does it mean if you "code fires"?
Back to top
View user's profile Send private message
DoobieKeebler
Moderator


Joined: 17 Jun 2005
Posts: 254
Location: 181°15'2.003"W, 93°5'16.956"N

PostPosted: Sep 15th, 2005 02:07 PM    Post subject: Reply with quote

In that usage it means that bit of code was invoked or was run. "Fired" ("fires") has a couple of nearly-similar meanings. When a user clicks a button and you've written code for the Click event, we sometimes say the program fires the click event. If something happens and I display a message box, I might say my code fires off a message box.

It's an old term, used by old programmers *cough*Doug*cough* and picked up by some of us who were taught by those old programmers. I'm not sure if people use that term anymore, but old slang dies hard.
_________________
Always take into account what a user would never ever in a million years do, because someone will.

"In theory, there's no difference between theory and practice. In practice, there is." -- Yogi Berra
Back to top
View user's profile Send private message
Under--Score
Regular


Joined: 19 Jul 2005
Posts: 88
Location: Location Location

PostPosted: Sep 19th, 2005 03:16 PM    Post subject: Reply with quote

With regards to you original post
"Private Sub Text1_KeyDown (KeyCode As Integer, Shift As Integer
Dim ShiftDown, AltDown, CtrlDown "

Does that mean all the events in the sub will happen whe you press ctrl alt del?

Could it also be employed as following:

Private Sub Text1_KeyDown (KeyCode as Integer, A as integer)

meaning that whenever the A key is pushed something happens? (This is not relevant to your problem. Rolleyes
Back to top
View user's profile Send private message
DoobieKeebler
Moderator


Joined: 17 Jun 2005
Posts: 254
Location: 181°15'2.003"W, 93°5'16.956"N

PostPosted: Sep 19th, 2005 03:46 PM    Post subject: Reply with quote

Under--Score wrote:
With regards to you original post
"Private Sub Text1_KeyDown (KeyCode As Integer, Shift As Integer
Dim ShiftDown, AltDown, CtrlDown "

Does that mean all the events in the sub will happen whe you press ctrl alt del?

No. I've got it set up so only one of the If statements is true. That's the only code that will run.

Under--Score wrote:
Could it also be employed as following:

Private Sub Text1_KeyDown (KeyCode as Integer, A as integer)

meaning that whenever the A key is pushed something happens? (This is not relevant to your problem. Rolleyes

No. The "A" comes from the KeyCode (If KeyCode = vbKeyA). Shift tells me in a roundabout way which, if any, of the "special keys" (i.e. Shift, Control, and Alt) are pressed. It also tells me if two or more of them are pressed at the same time.

Does that help any?
_________________
Always take into account what a user would never ever in a million years do, because someone will.

"In theory, there's no difference between theory and practice. In practice, there is." -- Yogi Berra
Back to top
View user's profile Send private message
Under--Score
Regular


Joined: 19 Jul 2005
Posts: 88
Location: Location Location

PostPosted: Sep 20th, 2005 10:41 AM    Post subject: Reply with quote

So if I put in:

Code:
Dim KeyCode as integer

If KeyCode = vbKeyA Then
MsgBox "You pressed the A key"
End If



that would work?
Back to top
View user's profile Send private message
DoobieKeebler
Moderator


Joined: 17 Jun 2005
Posts: 254
Location: 181°15'2.003"W, 93°5'16.956"N

PostPosted: Sep 20th, 2005 12:11 PM    Post subject: Reply with quote

Yes, your If block is exactly how it works. But you don't need the Dim statement. The variable is already DIMmed (in a way) in the Sub declaration.
_________________
Always take into account what a user would never ever in a million years do, because someone will.

"In theory, there's no difference between theory and practice. In practice, there is." -- Yogi Berra
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Visual Basic Forum for Visual Basic Programmers VB Forum Index » Knowledge Base All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Visual Basic Forum runs phpBB | Forum Template © iOptional
VB Resources | SSL | Visual Basic