Results 1 to 3 of 3

Thread: [RESOLVED] Very simple keypad application - how to?

  1. #1

    Thread Starter
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 2010
    Location
    Denmark
    Posts
    528

    Resolved [RESOLVED] Very simple keypad application - how to?

    I am in need of a small application, that will allways be topmost and will send keystrokes to the last active application (typically notepad or an explorer window). My problem is retrieving the current active form (or application), when my application is started up. Every search seems to return old and rather complex solutions using Win32 library functions, but I was kinda hoping, that .Net allowed for a neater and more simple solution.

    In short: I need a code example or link on how to retrieve the currently active application window from another application just starting up.

    Thank you in advance
    Tom
    In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)

  2. #2

    Thread Starter
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 2010
    Location
    Denmark
    Posts
    528

    Re: Very simple keypad application - how to?

    Recording processId of current and foreign process and using AppActivate and SendKeys.SendWait to transmit keystrokes is working but at an alarmingly slow rate, and many characters are skipped. Can it be true, that there isn't a decent possible approach to solve the problem of sending keystrokes between applications?
    In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)

  3. #3

    Thread Starter
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 2010
    Location
    Denmark
    Posts
    528

    Re: Very simple keypad application - how to?

    Just thought I'd post my solution here, in case anyone someone finds this thread with a search tool.

    The simple solution relies on an inherited form with extended params:
    vb Code:
    1. Public Class frmMain
    2.  
    3.     Inherits Form
    4.  
    5.     '----------------------------------------------------------------------------
    6.  
    7.     Private Const WS_EX_NOACTIVATE As Int32 = &H8000000
    8.  
    9.     '----------------------------------------------------------------------------
    10.  
    11.     Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams
    12.  
    13.         Get
    14.             Dim cp As System.Windows.Forms.CreateParams = MyBase.CreateParams
    15.  
    16.             cp.ExStyle = cp.ExStyle Or WS_EX_NOACTIVATE
    17.  
    18.             Return cp
    19.         End Get
    20.  
    21.     End Property
    22.  
    23.     Private Sub TransmitKey(ByVal sKey As String)
    24.  
    25.         'Backspace = "{BS}", Enter = "~"
    26.  
    27.         '.SendWait may be a better solution in some applications.
    28.         System.Windows.Forms.SendKeys.Send(sKey)
    29.  
    30.     End Sub
    31.  
    32.     'Button events consist only of a call to TransmitKey.
    33.  
    34. End Class

    The above solution is easily implemented and has the exact same functionality as the Windows keyboard.

    A better solution would be to allow activation of the window when clicking with the mouse on the border or title, and only preventing activation when the user clicks in the client area. This requires a rather complex series of calls to API functions, and even with Dan Appleman's guide, I found the task rather tedious and probably not worth the bother in most cases. I at least decided to settle for the simple case, since I found it impossible to prevent a significant loss of speed and some flickering of windows.

    If someone else feels up to the task, the key functions I used are listed below:
    * GetActiveWindow
    * GetWindowThreadProcessId
    * SetFocus
    * AttachThreadInput
    You'll need to override WndProc and listen for WM_ACTIVATEAPP with wParam zero and nonzero and WM_ACTIVE with wParam WA_CLICKACTIVE.

    Regards
    Tom
    In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)

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