SenKeys problem

HI!
I got some problem to simulate "Ctrl-Shift-2" with SendKeys

I register Ctrl-Shift-2

RegsiterHotKey(Me,HotKeyModifiers.Control Or HotKeyModifiers.Shift,Keys.D2)

But when trying to use SendKeys to simulate this I fail,

If I press Ctrl-Shift-2 it works as it should so the register works, but I can't get SendKeys to work.

Tips?
Public Enum HotKeyModifiers
            None = &H0
            Alt = &H1
            Control = &H2
            Shift = &H4
            Windows = &H8
        End Enum
 
 Public Function RegsiterHotKey(ByVal f As Form, ByVal Modifiers As HotKeyHelper.HotKeyModifiers, ByVal KeyCombo As Integer) As Boolean
            Me._keyId = f.GetHashCode()
            If RegisterHotKey(Me.Handle, Me._keyId, Modifiers, KeyCombo) Then
                RegisteredIDs.Add(Me._keyId)
                Return True
            Else
                Return False
            End If
        End Function

Open in new window

LVL 1
AWestEngAsked:
Who is Participating?
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
That's a possibility...I can't think of any other reason why it wouldn't work!

I've never worked with different languages before though...

So possibly SendKeys() is pushing something different on your keyboard and not causing the hotkey to be triggered?

What if we did this instead of SendKeys?


    Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, _
        ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
 
    Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" _
        (ByVal wCode As Integer, ByVal wMapType As Integer) As Integer
 
    Private Sub CtrlShift2()
        keybd_event(Keys.ControlKey, MapVirtualKey(Keys.ControlKey, 0), 0, 0) ' Key Down
        keybd_event(Keys.Menu, MapVirtualKey(Keys.Menu, 0), 0, 0) ' Key Down
        keybd_event(Keys.D2, MapVirtualKey(Keys.D2, 0), 0, 0) ' Key Down
        System.Threading.Thread.Sleep(100)
        keybd_event(Keys.D2, MapVirtualKey(Keys.D2, 0), 2, 0) ' Key Up
        keybd_event(Keys.Menu, MapVirtualKey(Keys.Menu, 0), 2, 0) ' Key Up
        keybd_event(Keys.ControlKey, MapVirtualKey(Keys.ControlKey, 0), 2, 0) ' Key Up
    End Sub

Open in new window

0
 
Wayne Taylor (webtubbs)Commented:
It's possible a control on the form has focus, thereby catching the key presses. Try setting the KeyPreview property of your form to True.

Wayne
0
 
AWestEngAuthor Commented:
nope, it doesn' thelp.
 
Is there something wrong with how I create the sendkey string
 SendKeys.Send("{^} or +({2})")
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
For Ctrl-Shift-2, you need:

    SendKeys.Send("(^+)2")
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You can also write it as:

    SendKeys.Send("^+2")
0
 
AWestEngAuthor Commented:
nope, it still dosen't work,,
 
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
It's something wrong with your setup then as I made a test app and it worked perfectly.  =\

Would you like me to post the bare bones code I used to test with?
0
 
AWestEngAuthor Commented:
absolutely, thx
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here is my simple test app:
Public Class Form1
 
    Private Enum HotKeyModifiers
        None = &H0
        Alt = &H1
        Control = &H2
        Shift = &H4
        Windows = &H8
    End Enum
 
    Private Ctrl_Shift_2 As Boolean = False
    Private Const WM_HOTKEY As Integer = &H312
 
    Private Declare Function RegisterHotKey Lib "user32" (ByVal handle As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Boolean
    Private Declare Function UnregisterHotKey Lib "user32" (ByVal handle As IntPtr, ByVal id As Integer) As Boolean
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Ctrl_Shift_2 = RegisterHotKey(Me.Handle, 0, HotKeyModifiers.Control Or HotKeyModifiers.Shift, Keys.D2)
    End Sub
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        SendKeys.Send("^+2")
    End Sub
 
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        Select Case m.Msg
            Case WM_HOTKEY
                HotKeyPressed(m.WParam.ToInt32)
 
        End Select
        MyBase.WndProc(m)
    End Sub
 
    Private Sub HotKeyPressed(ByVal id As Integer)
        Select Case id
            Case 0
                MessageBox.Show("Ctrl-Shift-2 Pressed!")
 
        End Select
    End Sub
 
    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        If Ctrl_Shift_2 Then
            UnregisterHotKey(Me.Handle, 0)
        End If
    End Sub
 
End Class

Open in new window

0
 
AWestEngAuthor Commented:
Thx,
the keys I send I do from another process (application), can that have something to do with my problem?
I will test your code Idle_Mind.
Thx
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
It shouldn't have any affect at all...
0
 
AWestEngAuthor Commented:
when I send the keys from another application it does not work, If press the keyborad It opens but not using send keys, If I use sendkeys from the application I want to react, it works,
 
But I need to send it from another application,
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I'll try using SendKeys() from another application.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Not sure what to tell ya...  =\

It worked for me using SendKeys from a DIFFERENT application.

I had the exact code above (minus the button click) in one app, and in another app I had:

    Public Class Form1

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            SendKeys.Send("^+2")
        End Sub

    End Class

When I press the button in the second app the first app pops up a MessageBox.
0
 
AWestEngAuthor Commented:
strange.. I'll have to check some more
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
BTW...I'm running Visual Studio 2008 on a Vista Home Premium 64-bit machine.
0
 
AWestEngAuthor Commented:
no, Ithat was not true, I did not work  sending it in the applcation that should react either.
So the only way for me is using the keyborad. Sendkeys does not work,
I use DevExpress forms, don't know if that has something to do with it.
Strange, when pressing the keybord it works prefect .
0
 
AWestEngAuthor Commented:
Using Windows XP, VS 2005
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Can you make a simple test app with the STOCK .Net forms (not the DevExpress) and see if that makes a difference?
0
 
AWestEngAuthor Commented:
I will test that and get back to you, thx man..
0
 
AWestEngAuthor Commented:
I did this , but it did not work..
 

test1.exe
Public Class Form1
 
    Private Enum HotKeyModifiers
        None = &H0
        Alt = &H1
        Control = &H2
        Shift = &H4
        Windows = &H8
    End Enum
 
    Private Ctrl_Shift_2 As Boolean = False
    Private Const WM_HOTKEY As Integer = &H312
 
    Private Declare Function RegisterHotKey Lib "user32" (ByVal handle As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Boolean
    Private Declare Function UnregisterHotKey Lib "user32" (ByVal handle As IntPtr, ByVal id As Integer) As Boolean
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Ctrl_Shift_2 = RegisterHotKey(Me.Handle, 0, HotKeyModifiers.Control Or HotKeyModifiers.Shift, Keys.D2)
    End Sub
 
 
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        Select Case m.Msg
            Case WM_HOTKEY
                MessageBox.Show("Hotkey Pressed!")
                HotKeyPressed(m.WParam.ToInt32)
 
        End Select
        MyBase.WndProc(m)
    End Sub
 
    Private Sub HotKeyPressed(ByVal id As Integer)
        Select Case id
            Case 0
                MessageBox.Show("Ctrl-Shift-2 Pressed!")
 
        End Select
    End Sub
 
    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        If Ctrl_Shift_2 Then
            UnregisterHotKey(Me.Handle, 0)
        End If
    End Sub
 
End Class
 
test2.exe
Public Class Form1
 
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        SendKeys.Send("^+2")
    End Sub
 
 
End Class

Open in new window

0
 
AWestEngAuthor Commented:
don't know if this has something to do with it, but I'm using a domain account on this computer.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I'm not sure why it won't work for you AWestEng...  =\

It works on both my laptops here at home (Vista & XP).  I don't have any other machines to test on as I'm a stay@home dad.

Sorry!
0
 
AWestEngAuthor Commented:
I can post the project so you can look at it..
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I don't have DevExpress (a third party control I assume?), so I'm pretty sure I wouldn't be able to get the project to work on my machine...
0
 
AWestEngAuthor Commented:
no I will post the test project with the stock forms..
0
 
AWestEngAuthor Commented:
I have a big problem with a MVP pattern, do you know anything about MVP?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Not sure...what does it stand for?  =\
0
 
AWestEngAuthor Commented:
http://www.easy-share.com/1903721308/SendKeys.zip
 
here is the sendkey code..
 
MVP = Model View Presenter (seperating models from GUI)
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
That project works perfectly for me...I push the button on the form and get two messageboxes!  =\
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Do you have another machine to test it on?
0
 
AWestEngAuthor Commented:
hehe. jepp, I have tested it on 2 XP machines and one vista machine, no one works..
hehe..
But I use Vs 2005 Pro on both machines..
And I have also converted it  to VS 2008, no lock luck there either...
Can it have something todo with that I use  Swedish keboard setting?
0
 
AWestEngAuthor Commented:
hehe, not that either..  keyboard works but not SendKeys or keybd_event
0
 
AWestEngAuthor Commented:
strange, Swedish keyboard is amost the same as english, only 3 more letter then you guys have.
 
0
 
AWestEngAuthor Commented:
http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send.aspx
Caution:
If your application is intended for international use with a variety of keyboards, the use of Send could yield unpredictable results and should be avoided.
0
 
AWestEngAuthor Commented:
hehehe..This works  Idle_Mind: )
Thx for all help, :)
/Ulf
 

    Const VK_SHIFT = &H10
    Const VK_CONTROL = &H11
 
    Private Sub CtrlShift2()
        keybd_event(VK_CONTROL, MapVirtualKey(VK_CONTROL, 0), 0, 0) ' Key Down
        keybd_event(VK_SHIFT, MapVirtualKey(VK_SHIFT, 0), 0, 0) ' Key Down
        keybd_event(Keys.D2, MapVirtualKey(Keys.D2, 0), 0, 0) ' Key Down
        System.Threading.Thread.Sleep(0)
        keybd_event(Keys.D2, MapVirtualKey(Keys.D2, 0), 2, 0) ' Key Up
        keybd_event(VK_SHIFT, MapVirtualKey(Keys.Menu, 0), 2, 0) ' Key Up
        keybd_event(VK_CONTROL, MapVirtualKey(VK_CONTROL, 0), 2, 0) ' Key Up
    End Sub

Open in new window

0
 
AWestEngAuthor Commented:
found this code,,,  if you want  it..
Const KEYEVENTF_KEYUP = &H2 ' Release key 
    Const VK_LBUTTON = &H1 'Left mouse button. 
    Const VK_RBUTTON = &H2 'Right mouse button. 
    Const VK_CANCEL = &H3 'Used for control-break processing. 
    Const VK_MBUTTON = &H4 '''Middle mouse button (3-button mouse). 
    Const VK_BACK = &H8
    Const VK_TAB = &H9
    Const VK_CLEAR = &HC
    Const VK_PRINT = &H2A
    Const VK_RETURN = &HD   'enter
    Const VK_SHIFT = &H10
    Const VK_CONTROL = &H11
    Const VK_SCROLL = &H91
    Const VK_MENU = &H12   'Alt (either one)
    Const VK_PAUSE = &H13
    Const VK_CAPITAL = &H14
    Const VK_ESCAPE = &H1B
    Const VK_STARTKEY = &H5B 'Start Menu key
    Const VK_SPACE = &H20
    Const VK_PRIOR = &H21 'Page up. 
    Const VK_NEXT = &H22 'Page down. 
    Const VK_END = &H23
    Const VK_HOME = &H24
    Const VK_LEFT = &H25
    Const VK_UP = &H26
    Const VK_RIGHT = &H27
    Const VK_DOWN = &H28
    Const VK_SELECT = &H29
    Const VK_INSERT = &H2D
    Const VK_DELETE = &H2E
    Const VK_HELP = &H2F
    Const VK_OEM_COMMA = &HBC     ',  comma
    Const VK_NUMLOCK = &H90 '      Num Lock
    Const KEYEVENTF_EXTENDEDKEY = &H1
    Const VK_0 = &H30
    Const VK_1 = &H31
    Const VK_2 = &H32
    Const VK_3 = &H33
    Const VK_4 = &H34
    Const VK_5 = &H35
    Const VK_6 = &H36
    Const VK_7 = &H37
    Const VK_8 = &H38
    Const VK_9 = &H39
    Const VK_A = &H41
    Const VK_B = &H42
    Const VK_C = &H43
    Const VK_D = &H44
    Const VK_E = &H45
    Const VK_F = &H46
    Const VK_G = &H47
    Const VK_H = &H48
    Const VK_I = &H49
    Const VK_J = &H4A
    Const VK_K = &H4B
    Const VK_L = &H4C
    Const VK_M = &H4D
    Const VK_N = &H4E
    Const VK_O = &H4F
    Const VK_P = &H50
    Const VK_Q = &H51
    Const VK_R = &H52
    Const VK_S = &H53
    Const VK_T = &H54
    Const VK_U = &H55
    Const VK_V = &H56
    Const VK_W = &H57
    Const VK_X = &H58
    Const VK_Y = &H59
    Const VK_Z = &H5A
    Const VK_NUMPAD0 = &H60
    Const VK_NUMPAD1 = &H61
    Const VK_NUMPAD2 = &H62
    Const VK_NUMPAD3 = &H63
    Const VK_NUMPAD4 = &H64
    Const VK_NUMPAD5 = &H65
    Const VK_NUMPAD6 = &H66
    Const VK_NUMPAD7 = &H67
    Const VK_NUMPAD8 = &H68
    Const VK_NUMPAD9 = &H69
    Const VK_MULTIPLY = &H6A    '     * (keypad)
    Const VK_ADD = &H6B
    Const VK_SEPARATER = &H6C
    Const VK_SEPARATOR = &H6C
    Const VK_SUBTRACT = &H6D
    Const VK_DECIMAL = &H6E
    Const VK_DIVIDE = &H6F
    Const VK_OEM_2 = &HBF  '      /
    Const VK_OEM_PERIOD = &HBE '.
    Const VK_F1 = &H70
    Const VK_F2 = &H71
    Const VK_F3 = &H72
    Const VK_F4 = &H73
    Const VK_F5 = &H74
    Const VK_F6 = &H75
    Const VK_F7 = &H76
    Const VK_F8 = &H77
    Const VK_F9 = &H78
    Const VK_F10 = &H79
    Const VK_F11 = &H7A
    Const VK_F12 = &H7B
    Const VK_F13 = &H7C
    Const VK_F14 = &H7D
    Const VK_F15 = &H7E
    Const VK_F16 = &H7F
    Const VK_F17 = &H80
    Const VK_F18 = &H81
    Const VK_F19 = &H82
    Const VK_F20 = &H83
    Const VK_F21 = &H84
    Const VK_F22 = &H85
    Const VK_F23 = &H86
    Const VK_F24 = &H87
    '****************
    Const VK_CRSEL = &HF7
    Const VK_EREOF = &HF9
    Const VK_EXECUTE = &H2B
    Const VK_EXSEL = &HF8
    Const VK_NONAME = &HFC
    Const VK_OEM_CLEAR = &HFE
    Const VK_PA1 = &HFD
    Const VK_PROCESSKEY = &HE5
    Const CAPSLOCK_ON = &H80    '  the capslock light is on.

Open in new window

0
 
AWestEngAuthor Commented:
Thx m8,
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Lol...

Looking back at the keybd_event() code I gave you previously...

I gave you Ctrl-Alt-2 instead of Shift-Ctrl-2!

Sorry about that.  =\
0
 
AWestEngAuthor Commented:
hehe.. thx for all help,,, you are great man.. :)
0
 
AWestEngAuthor Commented:
Do you know why after using
e.Cancel = True
when trying to close app the
WndProc method stops executing
0
 
AWestEngAuthor Commented:
or more corerct, the hotkey stops working
0
 
AWestEngAuthor Commented:
sorry not when just jusing e.Cancel
 
its when I do this..

Me.Opacity = 0
Me.ShowInTaskbar = False
 
Is there away to get the hotkey to work? even if I use
 
Me.Opacity = 0
Me.ShowInTaskbar = False
0
 
AWestEngAuthor Commented:
The problem can be solved by rregister the hotkey again after settings the Me.ShowInTaskbar = False
 
Don't know if this is the best way, but it works.. strange that WndProc does not get any hotkey messages else..
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I've never seen Hotkey code "stop" working under these conditions...I'll try it out myself and report back.
0
 
AWestEngAuthor Commented:
Having a problem with invoking from a event.
 
maybe you can solve it.
 
http://www.experts-exchange.com/Programming/Languages/.NET/Q_24145802.html#a23719630 
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.