473,320 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

KeyCode combination

Seth Schrock
2,965 Expert 2GB
I'm venturing (for the first time) into the realm of the KeyDown, KeyPress, KeyUp, etc. domain. I have successfully managed to get it to where it will recognize when I have pressed Ctrl + 1, Ctrl + 2, etc. I just want to make sure that I'm doing it in the way that is best. I had so much trouble figuring out how to do the multi-key testing that I'm just not sure. Here is my code:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  2. Dim ShiftKey As Integer
  3. Dim strCharacter As String
  4.  
  5. strCharacter = Chr(KeyCode)
  6.  
  7. ShiftKey = Shift And 7
  8.  
  9. If ShiftKey = 3 Then
  10.     Select Case strCharacter
  11.         Case 1 To 9
  12.             Call insertBlank(" %Blank" & strCharacter & "% ")
  13.  
  14.         Case "a"
  15.             Call insertBlank(" %Accused% ")
  16.  
  17.         Case "o"
  18.             Call insertBlank(" %Officer% ")
  19.  
  20.         Case "p"
  21.             Call insertBlank(" %PenalCode% ")
  22.  
  23.     End Select
  24. End If
  25.  
  26. End Sub
Expand|Select|Wrap|Line Numbers
  1. Private Sub insertBlank(strBlank As String)
  2. Dim strWording As String
  3.  
  4. With Me.LegalWording
  5.     .SetFocus
  6.     strWording = .Text
  7.     strWording = strWording & strBlank
  8.     .Value = strWording
  9.     .SelStart = Len(.Text)
  10. End With
  11.  
  12. End Sub
  13.  
I'm only testing for the shift of 3 (the VbCtrlMask + VbShiftMask) as I only need to know if both the Ctrl and Shift keys are pressed at the same time with an additional number or letter. I then am testing for the third key that was pressed (strCharacter). I then test for that value in the Select Case statement to tack onto the end of the text in LegalWording (a textbox on the form).

Again, this does work (surprisingly so since I did it on my own), but I just want to know if this is an acceptable way.
Dec 26 '12 #1

✓ answered by NeoPa

Checking the Shift value can be done as you have, by using the value 7 and storing it in a variable, but it could also be done as a single comparison :
Expand|Select|Wrap|Line Numbers
  1. If (Shift And (acShiftMask Or acCtrlMask)) = (acShiftMask Or acCtrlMask) Then
I generally recommend using predefined constants where available. It makes the code more easily read and understood.

In the same way, there is little need to store strCharacter, when you can use KeyCode instead, and compare with vbKey1, vbKey9, vbKeyA, vbKeyO and vbKeyP.

Otherwise, I would expect your code to work. It has no logic flaws in it. It looks pretty good TBF - especially for a first stab at it :-)

4 10199
NeoPa
32,556 Expert Mod 16PB
Checking the Shift value can be done as you have, by using the value 7 and storing it in a variable, but it could also be done as a single comparison :
Expand|Select|Wrap|Line Numbers
  1. If (Shift And (acShiftMask Or acCtrlMask)) = (acShiftMask Or acCtrlMask) Then
I generally recommend using predefined constants where available. It makes the code more easily read and understood.

In the same way, there is little need to store strCharacter, when you can use KeyCode instead, and compare with vbKey1, vbKey9, vbKeyA, vbKeyO and vbKeyP.

Otherwise, I would expect your code to work. It has no logic flaws in it. It looks pretty good TBF - especially for a first stab at it :-)
Dec 27 '12 #2
Seth Schrock
2,965 Expert 2GB
So if I'm understanding correctly, your line of code replaces line 9 and makes line 7 not needed (also removes any dim statement not used). Also, my Select case would be not testing strCharacter, but would be testing for KeyCode while each case would be VbKey1, VbKey2, etc.

Can I do a select case on VbKey1 to VbKey9 as I am for strCharacter or would I have to list each on individually?
Dec 27 '12 #3
Seth Schrock
2,965 Expert 2GB
Alright, I got brave and tried what I thought you meant out and of course, it worked. Thanks NeoPa. I'm starting to get things figured out and am getting braver with trying code :)

So here is what I have now (with the rest of the keycodes that I plan on using):
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  2.  
  3. 'Tests if Shift + Ctrl were pressed simultaneously
  4. If (Shift And (acShiftMask Or acCtrlMask)) = (acShiftMask Or acCtrlMask) Then
  5.  
  6.     'Tests other key pressed
  7.     Select Case KeyCode
  8.         Case vbKey1 To vbKey9 'For Blanks: number key pressed will be added to the end of "Blank"
  9.             Call insertBlank(" %Blank" & Chr(KeyCode) & "% ")
  10.  
  11.         Case vbKeyA
  12.             Call insertBlank(" %Accused% ")
  13.  
  14.         Case vbKeyO
  15.             Call insertBlank(" %Officer% ")
  16.  
  17.         Case vbKeyP
  18.             Call insertBlank(" %PenalCode% ")
  19.  
  20.         Case vbKeyD
  21.             Call insertBlank(" %day% ")
  22.  
  23.         Case vbKeyM
  24.             Call insertBlank(" %month% ")
  25.  
  26.         Case vbKeyY
  27.             Call insertBlank(" %year% ")
  28.  
  29.     End Select
  30. End If
  31.  
  32. End Sub
Dec 27 '12 #4
NeoPa
32,556 Expert Mod 16PB
Seth:
Can I do a select case on VbKey1 to VbKey9 as I am for strCharacter or would I have to list each on individually?
It seems you already know the answer is Yes Seth :-) Good for you.

You could also set up a constant to use for checking Ctrl and Shift :
Expand|Select|Wrap|Line Numbers
  1. Private Const conCtrlShiftMask As Long = acShiftMask Or acCtrlMask
  2.  
  3. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  4.     If (Shift And conCtrlShiftMask) = conCtrlShiftMask Then
Dec 27 '12 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: oeyvind toft | last post by:
How do I capture the ctrl keypress? Is it possible for js to intercept the ctrl-n combination and prevent the creation of a new IE window?? (Have 3 huge books on js and none of them mention...
1
by: Perttu Pulkkinen | last post by:
I have different functions that receive window.event as parameter. Functions are used like this: <input type="text" id="x" onkeypress="return onKeyCurrencyCheck(ev, 'x')" onblur...
6
by: rich_poppleton | last post by:
Help.... I've got a textarea where people type in a description. However for certain reasons we need to stop them typing !$*^ . I have a solution this which works fine in IE: function...
4
by: Thomas Christensen | last post by:
I'm trying to figure out what key the user pressed using a Danish keyboard layout. charCodeAt returns the correct number, but event.keyCode returns a wrong number, when using one of the keys that...
1
by: Diogo Alves - Software Developer | last post by:
I'm tring to override the combination all the combination like this one Ctrl + Shift + . I just can't catch it... I have already done this for ctrl + or Shift + but for both at same timeI can't...
2
by: Alberto | last post by:
Could you tell me witch is the difference between keycode, keyvalue and keydata in a keydown event? keyvalue is always the ascii code? thank you
3
by: Pugi! | last post by:
I got this (piece of) script from 'DHTML Utopia - Modern Webdesign - Using Javascript & DOM'. function aKeyWasPressed(e) { if (window.event) { var key = window.event.keyCode; } else { var key...
5
by: WilliamRLinden | last post by:
Hi world! we are pretty new to JavaScript and have been struggling for now 2 days on this problem ... We would appreciate mercy if anyone can give us some. Basically we are trying to simulate...
4
by: Nathan Sokalski | last post by:
I am writing a piece of code for an ASP.NET function that generates an onKeyPress JavaScript eventhandler that uses the event.keyCode / event.which properties. I have two situations that I would...
1
by: Peter Anthony | last post by:
This is a perfect example of how some information slips through the crack in MSDN2. There is NO explanation of what KeyValue is in MSDN2!! Sure, it has a page on it, here it is: ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.