write a new keyboard driver  for software keyboard(XP)

Posted on 2009-04-16
Last Modified: 2013-11-25
I have created a software keyboard. I need to know how to use this to control (office) applications instead of the regular keyboard. Maybe I need to figure out how to create plugins?
Question by:softspot
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    You mean you've created an "onscreen keyboard"?  (see image below)

    If so, you can use SendKeys.Send() to simulate the pressing of keys when your buttons are pushed:


    Author Comment

    Thanks for this.Yes, it is a kind of on-screen keyboard but it does not resemble the regular keyboard at all.  I have created an application which allows limited-physical-ability users to have an alternative to the physical keyboard. When it runs, it  lets the user use a one-click action to select any key.I don't use the physical keys at all. The app cycles rapidly (and endlessly) through a series of nested loops, presenting' keys' to the user, and the user makes the selection.  I need the result of their selection, (i.e. the key-press) to act as input to whatever other application they are running (typically MS-Word etc). I have looked at SendKey.Send() but the problem is that it sends the key to the 'Active' application. This means that the keyboard application then becomes non-active. This may seem trivial but I don't see an easy solution. Also, there is the problem of finding the active application. The SetForeGroundWindow function is a possibility but I think this means  'going Native' which is not easy to manage. At least, I have never  worked outside of managed code  (in .NET) and so I don't have any experience at all in using native code.
    I could give up and start over using C or Java but .NET made the development so far a real joy, and I am pushing our department to use .NET and I don't want to give up on this unless it really is not possible to find a solution.



    LVL 85

    Accepted Solution

    by: solution is to make so that your keyboard app CANNOT receive focus.  Then when the user selects a key you can use SendKeys.Send() and whatever app was the foreground app will receive the keystroke (because clicking your app doesn't steal focus).

    This is accomplished with the WS_EX_NOACTIVATE flag:

        "Windows 2000/XP: A top-level window created with this style does not become the foreground window when the user clicks it. The system does not bring this window to the foreground when the user minimizes or closes the foreground window."

    Here is an example of it being used:
    (the additional code in WndProc to handle WM_MOVING is necessary since the app cannot receive focus)
    Imports System.Runtime.InteropServices
    Public Class Form1
        Private Const WS_EX_NOACTIVATE = &H8000000
        Private Const WM_MOVING = &H216
        Private Structure RECT
            Public Left As Integer
            Public Top As Integer
            Public Right As Integer
            Public Bottom As Integer
        End Structure
        Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams
                Dim cp As CreateParams = MyBase.CreateParams
                cp.ExStyle = cp.ExStyle Or WS_EX_NOACTIVATE
                Return cp
            End Get
        End Property
        Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
            If m.Msg = WM_MOVING Then
                Dim r As RECT
                r = DirectCast(Marshal.PtrToStructure(m.LParam, GetType(RECT)), RECT)
                Me.Location = New Point(r.Left, r.Top)
            End If
        End Sub
    End Class

    Open in new window


    Author Comment

    Firstly apologies for the delay in responding. I got called away to deal with other pressures. Thanks for your expert help with this problem. I have been able to use this essentially to create a solution and I have built a neat  little software 'control' app which enables me to provide the 'keyboard' input that was the initial obhjective, but which also has opened up a whole host of other possibilities. An issue which you probably know about, is that there can be a short but unpredictable delay in the transmission between the non-focused app and the currently live app. This is not a major issue for what I am doing now, but it could be for some apps, and a developer woulld need to take care with this. I speculate that it is simply XPs scheduler at work in which case there is probably not much that can be done. Anyway, that is an issue for further exploration. I learn a little more about .NET most every day and your advice and knowledge has been very much appreciated.

    Author Closing Comment

    Please see my comments. You have ramped my education up another notch. Much appreciated.

    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Another limitation to be aware of is that the WS_EX_NOACTIVATE flag will NOT work WITHIN the SAME application.  When used on a form in the same application it no longer prevents focus from being taken.  Thus the keyboard app has to be its own executable...

    Author Comment

    Thanks. That's useful to know.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now