From: Dee Earley on 29 Apr 2010 09:01 On 29/04/2010 13:50, Boris Pauljev wrote: > Thanks for your reply! > You mean the results (what I make out of 1609 -> Shift+Alt+Ctrl) is > correct??? > > This would mean that "only" the 1609 is wrong, and not my code regarding > low byte order and high byte order and Shift and Alt and Ctrl. Looks like it. Just the 3 shift keys (and no normal key) would be 1792 (00000111-00000000) -- Dee Earley (dee.earley(a)icode.co.uk) i-Catcher Development Team iCode Systems (Replies direct to my email address will be ignored. Please reply to the group.)
From: Nobody on 29 Apr 2010 09:12 "Boris Pauljev" <nordiccoder(a)hotmail.com> wrote in message news:OMw5nZ45KHA.3880(a)TK2MSFTNGP04.phx.gbl... > Thank you. > I am having a serious problem... > > My code returns the wrong values for some reason, and I heavily under > pressure.... > > I have the following: > > dim iKeyCode% > iKeyCode = 1609 'originally it comes from "iKeyCode = VkKeyScanW(vKey)" > > Now I need to find out if this iKeyCode contains > > Shift, Ctrl, Alt > > To do this, I use the following: > > iShift = (iKeyCode And &HFF00&) \ &H100& > iVKKey = iKeyCode And &HFF& > > bShift = iShift And vbShiftMask 'vbShiftMask = 1 > bCtrl = iShift And vbCtrlMask 'vbCtrlMask = 2 > bAltGr = iShift And vbAltMask 'vbAltMask = 4 > > But this returns really strange values (=the wrong ones) > > Can anybody tell me where I went wrong? > > This is what MSDN writes about it: I wish that you have posted which API function you are using. Searching MSDN for phrases you included revealed that it's for VkKeyScan() function. When doing bit wise operations in VB, you need to account for negative numbers when dividing because the result may not be what you expect. The sign bit is the Most Significant Bit(MSB) in the number, so in Integer it's bit 15(counting from 0), and it's bit 31 for Long. So you need to convert the number to positive first before doing any calculations. Example for Integer: Dim iBit15 As Integer If iKeyCode >= 0 Then ' Positive number iBit15 = 0 Else ' Negative number iBit15 = 1 End If ' Remove the sign bit iKeyCode = iKeyCode And &H7FFF& ' Extract other bits here Without the above, you get strange values. For instance, if you have &H9001 Integer(-28671 Decimal), then dividing by &H100 would give &HFF91. This is normal and called signed division, but not what you want. VB cannot do Unsigned division, which C++ can, so you have to do the above to eliminate the sign bit first, then the result would be the same as doing unsigned division. Here is an example code: Option Explicit Private Sub Form_Load() Dim iKeyCode As Integer iKeyCode = &H9001 Debug.Print iKeyCode, Hex(iKeyCode) iKeyCode = iKeyCode \ &H100 Debug.Print iKeyCode, Hex(iKeyCode) End Sub Output: -28671 9001 -111 FF91 Note that if you wanted to extract the higher byte, which is "90" above, you get "91" instead if you don't eliminate the sign bit first.
From: Nobody on 29 Apr 2010 09:30 "Boris Pauljev" <nordiccoder(a)hotmail.com> wrote in message news:OMw5nZ45KHA.3880(a)TK2MSFTNGP04.phx.gbl... > iKeyCode = 1609 'originally it comes from "iKeyCode = VkKeyScanW(vKey)" Sorry, I didn't see that you posted the function name in the above line.
From: Nobody on 29 Apr 2010 09:44 "Boris Pauljev" <nordiccoder(a)hotmail.com> wrote in message news:OMw5nZ45KHA.3880(a)TK2MSFTNGP04.phx.gbl... > iKeyCode = 1609 'originally it comes from "iKeyCode = VkKeyScanW(vKey)" 1609 Decimal is &H649 Hex, or perhaps you meant to use &H1609. Your code to extract the necessary bits otherwise looks fine, except eliminating the sign bit as I mentioned earlier.
From: Boris Pauljev on 29 Apr 2010 10:04
Thank you. In my case, iKeyCode will never be negative, so I would rule the signed division problem. I did the following: Dim lAscW& lAscW = AscW("i") 'In fact it's not an "i" but a Turkish character that looks similar. I really wanted to say "1609". This number is being returned from VkKeyScanExW when I do the following: Dim lKeyboard& lKeyboard = GetKeyboardLayout(0) Dim lAscW& lAscW = AscW(sChar) iKeyCode = VkKeyScanExW(vKey, lKeyboard) I do know for sure that this character is not a shifted character, not an Alt character and not a Ctrl character, and it exists on this keyboard. Do you have any more ideas what might go wrong? I was so sure that I made a mistake in the bit calculation part... I could swear that there is not even any key that can be produced by hitting ALT+Ctrl+Shift... so this CANNOT be correct in my opinion. |