?
Solved

SenKeys problem

Posted on 2009-02-13
47
Medium Priority
?
468 Views
Last Modified: 2013-11-07
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

0
Comment
Question by:AWestEng
  • 29
  • 17
47 Comments
 
LVL 48

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 23631921
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
 
LVL 1

Author Comment

by:AWestEng
ID: 23633087
nope, it doesn' thelp.
 
Is there something wrong with how I create the sendkey string
 SendKeys.Send("{^} or +({2})")
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 23634722
For Ctrl-Shift-2, you need:

    SendKeys.Send("(^+)2")
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 23634730
You can also write it as:

    SendKeys.Send("^+2")
0
 
LVL 1

Author Comment

by:AWestEng
ID: 23640159
nope, it still dosen't work,,
 
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 23641013
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
 
LVL 1

Author Comment

by:AWestEng
ID: 23644441
absolutely, thx
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 23661247
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
 
LVL 1

Author Comment

by:AWestEng
ID: 23661472
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 23662212
It shouldn't have any affect at all...
0
 
LVL 1

Author Comment

by:AWestEng
ID: 23669680
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 23670926
I'll try using SendKeys() from another application.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 23670975
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
 
LVL 1

Author Comment

by:AWestEng
ID: 23671085
strange.. I'll have to check some more
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 23671302
BTW...I'm running Visual Studio 2008 on a Vista Home Premium 64-bit machine.
0
 
LVL 1

Author Comment

by:AWestEng
ID: 23671323
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
 
LVL 1

Author Comment

by:AWestEng
ID: 23671365
Using Windows XP, VS 2005
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 23671935
Can you make a simple test app with the STOCK .Net forms (not the DevExpress) and see if that makes a difference?
0
 
LVL 1

Author Comment

by:AWestEng
ID: 23674938
I will test that and get back to you, thx man..
0
 
LVL 1

Author Comment

by:AWestEng
ID: 23678681
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
 
LVL 1

Author Comment

by:AWestEng
ID: 23678840
don't know if this has something to do with it, but I'm using a domain account on this computer.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 23681940
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
 
LVL 1

Author Comment

by:AWestEng
ID: 23683801
I can post the project so you can look at it..
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 23683905
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
 
LVL 1

Author Comment

by:AWestEng
ID: 23684151
no I will post the test project with the stock forms..
0
 
LVL 1

Author Comment

by:AWestEng
ID: 23684162
I have a big problem with a MVP pattern, do you know anything about MVP?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 23685025
Not sure...what does it stand for?  =\
0
 
LVL 1

Author Comment

by:AWestEng
ID: 23685090
http://www.easy-share.com/1903721308/SendKeys.zip
 
here is the sendkey code..
 
MVP = Model View Presenter (seperating models from GUI)
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 23685230
That project works perfectly for me...I push the button on the form and get two messageboxes!  =\
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 23685233
Do you have another machine to test it on?
0
 
LVL 1

Author Comment

by:AWestEng
ID: 23685332
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
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 23685425
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
 
LVL 1

Author Comment

by:AWestEng
ID: 23685530
hehe, not that either..  keyboard works but not SendKeys or keybd_event
0
 
LVL 1

Author Comment

by:AWestEng
ID: 23685556
strange, Swedish keyboard is amost the same as english, only 3 more letter then you guys have.
 
0
 
LVL 1

Author Comment

by:AWestEng
ID: 23685943
0
 
LVL 1

Author Comment

by:AWestEng
ID: 23686097
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
 
LVL 1

Author Comment

by:AWestEng
ID: 23686261
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
 
LVL 1

Author Comment

by:AWestEng
ID: 23686275
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
 
LVL 1

Author Closing Comment

by:AWestEng
ID: 31546528
Thx m8,
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 23686459
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
 
LVL 1

Author Comment

by:AWestEng
ID: 23704167
hehe.. thx for all help,,, you are great man.. :)
0
 
LVL 1

Author Comment

by:AWestEng
ID: 23704377
Do you know why after using
e.Cancel = True
when trying to close app the
WndProc method stops executing
0
 
LVL 1

Author Comment

by:AWestEng
ID: 23704381
or more corerct, the hotkey stops working
0
 
LVL 1

Author Comment

by:AWestEng
ID: 23704402
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
 
LVL 1

Author Comment

by:AWestEng
ID: 23704489
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 23706032
I've never seen Hotkey code "stop" working under these conditions...I'll try it out myself and report back.
0
 
LVL 1

Author Comment

by:AWestEng
ID: 23719718
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question