sending key strokes

hello there,
i need to know how to send key strokes as it were type from the keyboard.(no sendkeys) and also how to disable the keyboard inputs from entering into the system? please help me with this. thanks in advance.

Chris.

Comments

  • Do you want to send key strokes to a different process? If so was the process spawned from the process sending the key strokes? Are you trying to "nullify" the keyboard input for the process you are coding, or for some other process (analogous to the sending key strokes).

    For now I'll assume that you want to catch keyboard input to another process, possibly modify it and send your own keyboard input. You need to set a "Hook" for the window's message that correlates to the keypress event. This is done with the API. I don't remember the specifics, but last time I worked with hooks it was fairly intuitive. In the hook callback, I believe you can edit the content of the message before it reaches its destination. If you need to send your own. You need to create a window's message and send it to window within the process that you'd like to send the keystroke to.

    SendKeys does a great job of simplifying the process of creating your own message. Why can't you use SendKeys?

    -ray
  • well, i need to send my own key strokes as it were type from the key board, to different applications. for example i need to send alt + f to a minimized ms word application to invoke the file menu. sendkeys was not much useful. thanks for ur help and can u give me a solution ???
    chris.
  • : well, i need to send my own key strokes as it were type from the key board, to different applications. for example i need to send alt + f to a minimized ms word application to invoke the file menu. sendkeys was not much useful. thanks for ur help and can u give me a solution ???
    : chris.
    :
    Sendkeys wasn't useful, because it sends to the active application and by default a minimized app isn't active.

    The solution really is to use the api's message manipulation functions. Look at "SetWindowsHookEx" in any relevant documentation (msdn, .net help, msdn online). This will get you started on catching the windows messages.

    Another promising lead, I found something called an "Event Filter" in the .net documentation. I don't know if this is a representation of the Windows Hook functions but its worth looking into.

    -ray
  • : : well, i need to send my own key strokes as it were type from the key board, to different applications. for example i need to send alt + f to a minimized ms word application to invoke the file menu. sendkeys was not much useful. thanks for ur help and can u give me a solution ???
    : : chris.
    : :
    : Sendkeys wasn't useful, because it sends to the active application and by default a minimized app isn't active.
    :
    : The solution really is to use the api's message manipulation functions. Look at "SetWindowsHookEx" in any relevant documentation (msdn, .net help, msdn online). This will get you started on catching the windows messages.
    :
    : Another promising lead, I found something called an "Event Filter" in the .net documentation. I don't know if this is a representation of the Windows Hook functions but its worth looking into.
    :
    : -ray
    :

    ' API declares
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
    Private Declare Function SetForegroundWindow Lib "user32" Alias "SetForegroundWindow" (ByVal hwnd As Integer) As Integer

    ' send keys to selected app
    'reusable sendkeys sub

    Sub SendKeysToApplication(ByVal appTitle As String, ByVal keys As String)
    ' Find the other application.
    Dim hWnd As Integer = FindWindow(Nothing, appTitle)
    ' Exit if not found.
    If hWnd <= 0 Then
    MessageBox.Show("Application not found", "Error", _
    MessageBoxButtons.OK, MessageBoxIcon.Error)
    Exit Sub
    End If
    ' Make it the active application.
    SetForegroundWindow(hWnd)
    ' Send the keys and wait.
    SendKeys.SendWait(keys)
    End Sub

    SendKeysToApplication("Word" , "{alt} {f4}")
  • i got my computer broke down and sorry i am late.. thanks a lot for this help... Tsikman .. will it be possible to activate menus or type Text, align them in a word document..? with this.. ?

  • : i got my computer broke down and sorry i am late.. thanks a lot for this help... Tsikman .. will it be possible to activate menus or type Text, align them in a word document..? with this.. ?

    If you need to continue using these shortkeys from word......

    I have used this code to send keys from within the active .net program, that of which you are building. I think that at the time you use your shortkey, focus will change to the program that you have specified to accept the commands. If you want to be able to handle these things from the newly focused application for eg. (Word) you ll have to work with global keyboard hooks. Global keyboard hooks catch keyboard events from all threads not just the currently focused thread. That will trap the keystrokes at any time that you want to perform the actions , not just from the .net app. Be aware that if your applications use the same shortkeys there may be some unexpected behavior. The things that you should look at in the latter case, are as follows

    SetWindowsHookEx
    UnhookWindowsHookEx
    GetAsyncKeyState
    CallNextHookEx

    however the rules that apply to .net and vb 6 are different so you should avoid the reading vb 6 info. in those areas.
    http://www.developer.com/net/net/print.php/2193301
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories