I want to create a simple virtual keyboard in win7 as
| --- | --- | --- | 9 char in first block (abcdefgijk)
| --- | --- | --- |
| --- | --- | --- |
there is a button "9" when the button "First button" switches to another view.
| --- | --- | --- |
abc
| --- | --- | --- |
efg
| --- | --- | --- |
ijk
Now I am confused about how to press a button, for example, a keystroke keyboard can generate char output to another application. I use
System.Windows.Forms.SendKeys.SendWait(sendString);
but that will not work. I know sendString is correct, because I view it as Console.WriteLine(sendString);.
Another question: the focus will not change to the button when the button is pressed.
Does anyone have a solution how to implement this keyboard?
Thank!
,
, . , , . 2 . . !!
namespace WpfApplication5
{
public partial class UserControl1 : UserControl
{
[DllImport("user32.dll")]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
[DllImport("user32.dll", SetLastError = true)]
public static extern UInt32 GetWindowLong(IntPtr hWnd, int nIndex);
private IInputElement focusedInputElement;
private Window parentWindow;
private List<Button> keyCollection = new List<Button>();
public UserControl1(Window parent)
{
this.parentWindow = parent;
this.setupKeyboardControl();
}
public UserControl1(IInputElement elementToFocusOn)
{
this.focusedInputElement = elementToFocusOn;
this.setupKeyboardControl();
}
private void setupKeyboardControl()
{
InitializeComponent();
this.addAllKeysToInternalCollection();
this.installAllClickEventsForCollection(this.keyCollection);
}
private void addAllKeysToInternalCollection()
{
Console.WriteLine("Run at here");
this.keyCollection.Add(A);
this.keyCollection.Add(B);
}
private void installAllClickEventsForCollection(List<Button> keysToInstall)
{
foreach (Button buttonElement in keysToInstall)
{
buttonElement.Click += new RoutedEventHandler(buttonElement_Click);
}
}
void buttonElement_Click(object sender, RoutedEventArgs e)
{
String sendString = "";
try
{
e.Handled = true;
sendString = ((Button)sender).CommandParameter.ToString();
if (!String.IsNullOrEmpty(sendString))
{
if (sendString.Length > 1)
{
sendString = "{" + sendString + "}";
}
if (this.focusedInputElement != null)
{
Console.WriteLine("1",sendString);
Keyboard.Focus(this.focusedInputElement);
this.focusedInputElement.Focus();
}
System.Windows.Forms.SendKeys.SendWait(sendString);
Console.WriteLine(sendString);
}
}
catch (Exception)
{
Console.WriteLine("Could not send key press: {0}", sendString);
}
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
if (this.parentWindow != null)
{
IntPtr HWND = new WindowInteropHelper(this.parentWindow).Handle;
Console.WriteLine("Run in UserControl load");
int GWL_EXSTYLE = (-20);
GetWindowLong(HWND, GWL_EXSTYLE);
SetWindowLong(HWND, GWL_EXSTYLE, (IntPtr)(0x8000000));
}
}
}
}