Results 1 to 8 of 8

Thread: How can i send an "CTRL" -i command using PostMessage (or SendMessage) ?

  1. #1
    vb.elmar
    Guest

    How can i send an "CTRL" -i command using PostMessage (or SendMessage) ?

    Hello
    Sending normal chars works fine, but how can i send an "CTRL" -i command
    using PostMessage (or SendMessage) ?


    Sample:






    Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Const WM_KEYDOWN = &H100
    Private Const WM_KEYUP = &H101
    Private Const WM_CHAR = &H102
    Private Const WM_SYSKEYDOWN = &H104
    Private Const WM_SYSKEYUP = &H105
    Private Const VK_SHIFT = &H10
    Private Const VK_CONTROL = &H11
    Dim hWn


    Private Sub Form_Load()
    Visible = False
    hWn = FindWindow("ieframe", vbNullString) 'Find the IExplorer window (Clsname="IEFrame")
    'sending CTRL-i (Toggels the "Favorites" Window visible-invisible-visible, in German IExplorer)
    PostMessage hWn, WM_KEYDOWN, VK_CONTROL, 0
    PostMessage hWn, WM_KEYDOWN, Asc("I"), 0
    PostMessage hWn, WM_KEYUP, Asc("I"), 0
    PostMessage hWn, WM_KEYUP, VK_CONTROL, 0
    End Sub


    Sub sendAlt_DN()
    'sends ALT-D-N (=File Menu, in German IExplorer)
    PostMessage hWn, WM_SYSKEYDOWN, Asc("D"), &H20200001 'Alt-D Down
    t = Timer: While Timer - t < 0.8: DoEvents: Wend
    PostMessage hWn, WM_SYSKEYDOWN, Asc("N"), &H20200001 'Alt-N Down
    End Sub










    <img src="http://members.theglobe.com/HenryForts/Pusteblume.gif"><img src="http://members.theglobe.com/HenryForts/Pusteblume.gif">


    Virtual-Key Codes

    Const VK_LBUTTON = &H1
    Const VK_RBUTTON = &H2
    Const VK_CANCEL = &H3
    Const VK_MBUTTON = &H4 ' NOT contiguous with L RBUTTON
    Const VK_BACK = &H8
    Const VK_TAB = &H9
    Const VK_CLEAR = &HC
    Const VK_RETURN = &HD
    Const VK_SHIFT = &H10
    Const VK_CONTROL = &H11
    Const VK_MENU = &H12
    Const VK_PAUSE = &H13
    Const VK_CAPITAL = &H14
    Const VK_ESCAPE = &H1B
    Const VK_SPACE = &H20
    Const VK_PRIOR = &H21
    Const VK_NEXT = &H22
    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_PRINT = &H2A
    Const VK_EXECUTE = &H2B
    Const VK_SNAPSHOT = &H2C
    Const VK_INSERT = &H2D
    Const VK_DELETE = &H2E
    Const VK_HELP = &H2F

    VK_0 30 0 key
    VK_1 31 1 key
    VK_2 32 2 key
    VK_3 33 3 key
    VK_4 34 4 key
    VK_5 35 5 key
    VK_6 36 6 key
    VK_7 37 7 key
    VK_8 38 8 key
    VK_9 39 9 key
    3A–40 Undefined
    VK_A 41 A key
    VK_B 42 B key
    VK_C 43 C key
    VK_D 44 D key
    VK_E 45 E key
    VK_F 46 F key
    VK_G 47 G key
    VK_H 48 H key
    VK_I 49 I key
    VK_J 4A J key
    VK_K 4B K key
    VK_L 4C L key
    VK_M 4D M key
    VK_N 4E N key
    VK_O 4F O key
    VK_P 50 P key
    VK_Q 51 Q key
    VK_R 52 R key
    VK_S 53 S key
    VK_T 54 T key
    VK_U 55 U key
    VK_V 56 V key
    VK_W 57 W key
    VK_X 58 X key
    VK_Y 59 Y key
    VK_Z 5A Z key
    VK_LWIN 5B Left Windows key on a Microsoft Natural Keyboard
    VK_RWIN 5C Right Windows key on a Microsoft Natural Keyboard
    VK_APPS 5D Applications key on a Microsoft Natural Keyboard
    5E–5F Undefined
    VK_NUMPAD0 60 Numeric keypad 0 key
    VK_NUMPAD1 61 Numeric keypad 1 key
    VK_NUMPAD2 62 Numeric keypad 2 key
    VK_NUMPAD3 63 Numeric keypad 3 key
    VK_NUMPAD4 64 Numeric keypad 4 key
    VK_NUMPAD5 65 Numeric keypad 5 key
    VK_NUMPAD6 66 Numeric keypad 6 key
    VK_NUMPAD7 67 Numeric keypad 7 key
    VK_NUMPAD8 68 Numeric keypad 8 key
    VK_NUMPAD9 69 Numeric keypad 9 key
    VK_MULTIPLY 6A Multiply key
    VK_ADD 6B Add key
    VK_SEPARATOR 6C Separator key
    VK_SUBTRACT 6D Subtract key
    VK_DECIMAL 6E Decimal key
    VK_DIVIDE 6F Divide key
    VK_F1 70 F1 key
    VK_F2 71 F2 key
    VK_F3 72 F3 key
    VK_F4 73 F4 key
    VK_F5 74 F5 key
    VK_F6 75 F6 key
    VK_F7 76 F7 key
    VK_F8 77 F8 key
    VK_F9 78 F9 key
    VK_F10 79 F10 key
    VK_F11 7A F11 key
    VK_F12 7B F12 key
    VK_F13 7C F13 key
    VK_F14 7D F14 key
    VK_F15 7E F15 key
    VK_F16 7F F16 key
    VK_F17 80H F17 key
    VK_F18 81H F18 key
    VK_F19 82H F19 key
    VK_F20 83H F20 key
    VK_F21 84H F21 key
    VK_F22 85H F22 key
    VK_F23 86H F23 key
    VK_F24 87H F24 key
    88–8F Unassigned
    VK_NUMLOCK 90 NUM LOCK key
    VK_SCROLL 91 SCROLL LOCK key
    VK_LSHIFT 0xA0 Left SHIFT
    VK_RSHIFT 0xA1 Right SHIFT
    VK_LCONTROL 0xA2 Left CTRL
    VK_RONTROL 0xA3 Right CTRL
    VK_LMENU 0xA4 Left ALT
    VK_RMENU 0xA5 Right ALT
    BA-C0 OEM-specific; reserved. See following tables.
    C1-DA Unassigned
    DB-E2 OEM-specific; reserved. See following tables.
    E3 – E4 OEM-specific
    E5 Unassigned
    E6 OEM-specific
    E7-E8 Unassigned
    E9-F5 OEM-specific
    VK_ATTN F6 ATTN key
    VK_CRSEL F7 CRSEL key
    VK_EXSEL F8 EXSEL key
    VK_EREOF F9 Erase EOF key
    VK_PLAY FA PLAY key
    VK_ZOOM FB ZOOM key
    VK_NONAME FC Reserved for future use
    VK_PA1 FD PA1 key
    VK_OEM_CLEAR FE CLEAR key


    OEMs should make special note of the VK key ranges reserved for OEM-specific use: 2A, DB–E4, E6, and E9–F5.

    In addition to the VK key assignments in the previous table, Microsoft has assigned the following OEM VK keys.

    Symbolic constant Value (Hex) Mouse or keyboard equivalent
    VK_OEM_SCROLL 0x91
    VK_OEM_1 0xBA “;:” for US
    VK_OEM_PLUS 0xBB “+” any country
    VK_OEM_COMMA 0xBC “,” any country
    VK_OEM_MINUS 0xBD “-” any country
    VK_OEM_PERIOD 0xBE “.” any country
    VK_OEM_2 0xBF “/?” for US
    VK_OEM_3 0xC0 “`~” for US
    VK_OEM_4 0xDB “[{” for US
    VK_OEM_5 0xDC “\|” for US
    VK_OEM_6 0xDD “]}” for US
    VK_OEM_7 0xDE “'"” for US
    VK_OEM_8 0xDF
    VK_OEM_AX 0xE1 AX key on Japanese AX keyboard
    VK_OEM_102 0xE2 “<>” or “\|” on RT 102-key keyboard







    =======================================================================

    If you want to send the string "Fo", post the following messages:


    WM_KEYDOWN WKeyShift
    WM_KEYDOWN WKeyF
    WM_KEYUP WKeyF
    WM_KEYUP WKeyShift
    WM_KEYDOWN WKeyO
    WM_KEYUP WKeyO


    The destination application messageloop watches the keydown and keyup
    messages and posts a WM_CHAR message for each ASCII keydown message it se=
    es.
    Also keystrokes messages are turned into WM_SYSKEYDOWN, WM_SYSCHAR, and
    WM_SYSKEYUP messages for those that had the ALT key down at the time.


    If you use SendMessage the messages are not translated in WM_CHAR.



    ========================================================================
    · Nonsystem Keystroke

    WM_KEYDOWN a key has been pressed
    WM_KEYUP a key has been released
    WM_CHAR contains the character code of a pressed key


    · System Keystroke (While the Alt key is pressed)

    WM_SYSKEYDOWN system key has been pressed
    WM_SYSKEYUP system key has been released
    WM_SYSCHAR Contains the character code while the Alt key is pressed

    · A program normally ignore the system keystroke messages and pass them to DefWindowProc()


    ===========================
    The lParam Variable

    This 32-bit variable, with 6 fields, is passed to the WndProc()

    Bit 0-15 Repeat count; the number of times of keystroke is repeated
    Bit 16-23 8-bit OEM scan code
    Bit 24 Extended key flag: 1 – extended key; 0 -- otherwise
    Bit 25-28 Reserved
    Bit 29 Context code: 1=Alt key is pressed; 0=otherwise (100000000000000000000000000000=&H20000000)
    Bit 30 Previous key state: 0=previous key up, 1=previous key down (=40000000)
    Bit 31 Transition state: 0 – WM_KEYDOWN, 1 – WM_KEYUP
    ========================================================================






    <img src="http://www.bz-berlin.de/musik/awards/grandprix/gifs/bb19raab.gif">

  2. #2
    Addicted Member
    Join Date
    Apr 2000
    Location
    England
    Posts
    246
    just set the system state of control instead:

    Call keybd_event(VK_CONTROL, MapVirtualKey(VK_CONTROL,0),0,0)

    and to release the Control key:

    Call keybd_event(VK_CONTROL, MapVirtualKey(VK_CONTROL,0),KEYEVENTF_KEYUP,0)

  3. #3
    vb.elmar
    Guest
    I know keybd_event. It must work using PostMessage or SendMessage.

  4. #4
    Addicted Member
    Join Date
    Apr 2000
    Location
    England
    Posts
    246
    Depends on the app. sending a Ctrl Down message to a program that checks the system state of special keys will not do a thing.

    Why do you want to use postmessage anyway. whats so special about it?

    you can still use the postmessage to send the "I" character which will work as ctrl-I if you have used keybd_event.

    you cannot set the state of special keys to one window, you have to set it to the system as a whole, unless the app can reconise the control down messages (most cannot)

  5. #5
    vb.elmar
    Guest
    IE accepts "ALT" messages posted by sendmessage or postmessage.


    Example:
    Sub sendAlt_DN()
    'sends ALT-D-N (=File Menu, in German IExplorer)
    PostMessage hWn, WM_SYSKEYDOWN, Asc("D"), &H20200001 'Alt-D Down
    t = Timer: While Timer - t < 0.8: DoEvents: Wend
    PostMessage hWn, WM_SYSKEYDOWN, Asc("N"), &H20200001 'Alt-N Down
    End Sub



    sends ALT-D to the Internet Explorer. Think, that is possible
    to send a "CTRL" message too.

  6. #6
    vb.elmar
    Guest
    >Why do you want to use postmessage anyway. whats so special about it?

    specifying handle of the target window.(no need SetForegroundWindow !)



    Postmessage can simulate an "ALT" -keydown ! So it can simulate
    a "CTRL" or "Shift" keydown too ***i think** (???)

  7. #7
    Addicted Member
    Join Date
    Apr 2000
    Location
    England
    Posts
    246
    again, it all depends on how the program intrepates the messages, Alt controls are a bit different

    postmessage can send a control down message to a window, but it will only work if the window accepts it!

    if you use the keyboard event API, you can still send a Ctrl-I without setforegroundwindow. just use keyboard event to set the SYSTEM state of control, then use sendmessage to post the I character to the Window in the background.

    Call keybd_event(VK_CONTROL, MapVirtualKey(VK_CONTROL,0),0,0)
    PostMessage hWn, WM_KEYDOWN, Asc("I"), 0
    Call keybd_event(VK_CONTROL, MapVirtualKey(VK_CONTROL,0) ,KEYEVENTF_KEYUP,0)

    that code will work, doesn't matter if the window is in the foreground or not

  8. #8
    vb.elmar
    Guest
    OK, it seems that sending "ALT" is a really special
    case, and sending "SHIFT" or "CTRL" is something
    complete different !

    Fine, that i don't need setforegroundwindow.

    I will use keybd_event and postmessage now. -thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width