BLeRg

In TASM I would simply check the BIOS Shift Flags and Extended Shift Flags. There are an LShiftKey And RShiftKey, but Control.Keys only returns the "BIOS Shift Flags" without the Extended Shift Flags. I would like it if my program could keep all its functionality as I "translate" it from assembly.

Thanks.



Re: Visual Basic Express Edition How do I distinguish between LShift and RShift keys.

Tall Dude

Maybe not the best way but:

' With form1 'keypreview' property set to TRUE

Public Class Form1

Dim ShiftSide As IntPtr

Dim locked As Boolean = False

Private Const key_down As Integer = &H100

Private Const key_up As Integer = &H101

Private Sub Form1_KeyDown(ByVal sender As System.Object, _

ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

If e.Modifiers = Keys.Shift AndAlso e.KeyCode <> 16 Then

If ShiftSide = CType(&H360001, IntPtr) Then

MsgBox("A right shifted " & e.KeyCode.ToString & " was pressed")

Else

MsgBox("A left shifted " & e.KeyCode.ToString & " was pressed")

End If

End If

End Sub

Protected Overrides Sub DefWndProc _

(ByRef m As System.Windows.Forms.Message)

MyBase.DefWndProc(m)

If m.Msg = key_down AndAlso Not locked Then

ShiftSide = m.LParam

locked = True

End If

If m.Msg = key_up Then

ShiftSide = Nothing

locked = False

End If

End Sub

End Class






Re: Visual Basic Express Edition How do I distinguish between LShift and RShift keys.

jo0ls

Or... GetKeyState

Public Class NativeMethods

Private Const VK_LSHIFT As UInteger = &HA0

Private Const VK_RSHIFT As UInteger = &HA1

<DllImport("user32.dll")> _

Private Shared Function GetKeyState(ByVal nVirtKey As UInteger) As Short

End Function

<Flags()> _

Public Enum ShiftKeyState As Integer

LeftDown

RightDown

End Enum

Public Shared Function ShiftKeyStates() As ShiftKeyState

' If high bit of the return value is set then key is down.

' Mask off the high bit, and shift it right to bit 1 for LShift, and bit 2 for RShift

Dim left As ShiftKeyState = CType((GetKeyState(VK_LSHIFT) And &H8000) >> 16, ShiftKeyState)

Dim right As ShiftKeyState = CType((GetKeyState(VK_RSHIFT) And &H8000) >> 15, ShiftKeyState)

Return left Or right ' I can't get Both to work, probably my keyboard.

End Function

End Class





Re: Visual Basic Express Edition How do I distinguish between LShift and RShift keys.

BLeRg

Well, it was a different approach, but it had the same effect of returning both keys identically (16) as opposed to (161) or (160), and I discovered a second key had to be pressed as well. It was worth a shot.

Thanks.





Re: Visual Basic Express Edition How do I distinguish between LShift and RShift keys.

Tall Dude

This below will respond to just the shift key being pressed.

If used in conjuction with regular key event handling methods,

The DefWndProc may fire last.

Public Class Form1

Private Const key_down As Integer = &H100

Private Const key_up As Integer = &H101

Protected Overrides Sub _

DefWndProc(ByRef m As System.Windows.Forms.Message)

MyBase.DefWndProc(m)

If m.Msg = key_down Then

If m.LParam = CType(&H360001, IntPtr) Then

MsgBox("Right Shift")

End If

If m.LParam = CType(&H2A0001, IntPtr) Then

MsgBox("Left Shift")

End If

End If

End Sub

Private Sub form1_Load(ByVal sender As Object, _

ByVal e As System.EventArgs) Handles Me.Load

Me.KeyPreview = True

End Sub

End Class






Re: Visual Basic Express Edition How do I distinguish between LShift and RShift keys.

BLeRg

Tall Dude,

Cool! I set a break point in your example to determine the rest of the keys excluding the Alts which don't raise a key down event. Any more good ideas

If I recall correctly, the Menu Bar shortcuts use the Alt keys. I may experiment with that to see if its possible to use an invisable menu bar of some sort, unless there is a better way.




Re: Visual Basic Express Edition How do I distinguish between LShift and RShift keys.

BLeRg

Sorry, it didn't run correctly. I think you might be using Windows CE, but I'm too new to Windows and not at all familiar with CE to make anything other than a guess.

Thanks.





Re: Visual Basic Express Edition How do I distinguish between LShift and RShift keys.

Tall Dude

Yes, use 'debug.writeline'.

It's quicker than using a breakpoint.

Then have it write 'm' messages and

'lparams'.

And maybe someone can post a link to

those 'magic' hex numbers.

I know they are supposed to be in the header

files in some SDKs, but a reference link to

just look 'em up would be nice.