Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Virtual keyboard on Top

Tags:

c#

wpf

touch

We are going to create a virtual keyboard (on screen keyboard) with customized keys (no Ctrl, Alt and ...). The problem is when we set the application to be Topmost="Ture" then it is not possible to find the last active application's window to send the selected key to it. (The keyboard application is now the active one.) We did a few search but couldn't find anything helpful.

like image 957
Babak Avatar asked Nov 23 '25 02:11

Babak


1 Answers

Keep a handle to the last window before you set the prperty to true, take a look at GetForegroundWindow() or GetActiveWindow(), then use a the SetActiveWindow() to set it back after done with your keyboard application.

using System;
using System.Runtime.InteropServices;

namespace Foreground {
  class GetForegroundWindowTest {

    [DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
    public static extern IntPtr GetForegroundWindow();

    public static void Main(string[] args){
        IntPtr fg = GetForegroundWindow(); //use to keep the last active window
        // set the topmost property to your keyboard        
        //Set fg to be active again when needed using SetActiveWindow()
    }
  }
}
like image 130
CloudyMarble Avatar answered Nov 24 '25 16:11

CloudyMarble



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!