John M. Henreick John M. Henreick - 3 months ago 17
C# Question

Input simulator C# WPF Keeps throwing me errors when trying to use it

So I just recently transferred over from

WinForms
to
WPF
and I've got a grasp knowledge of it now and I tried using the
sendkey
class but as you already know thats not possible in WPF, so I did some research and googled for a while and I stumbled upon the
inputsimulator
.

https://inputsimulator.codeplex.com/

And this code should work just fine with WPF as far as I know, I've added the reference and I've added the namespace

public void PressTheSpacebar()
{
InputSimulator.SimulateKeyPress(VirtualKeyCode.SPACE);
}


but it keeps throwing me these 2 errors


The type 'InputSimulator' exists in both 'InputSimulator,
Version=0.1.0.0, Culture=neutral, PublicKeyToken=null' and
'WindowsInput, Version=1.0.4.0, Culture=neutral,
PublicKeyToken=9b287f7dc5073cad'


and


'VirtualKeyCode' is an ambiguous reference between
'WindowsInput.Native.VirtualKeyCode' and 'WindowsInput.VirtualKeyCode'


A more visual reference would be this

Here is a picture of the errors:

enter image description here

Answer

You're creating an ambiguous reference. That means the compiler is not sure from where the method should be called. InputSimulator exists in both namespaces. So the compiler cannot compile properly.

To fix the issue, you need to remove any one of the namespaces. Probably remove System.Windows.Input.

EDIT : I have checked your code. Just these namespaces are required. Remove all other namespaces and build your code. It should work fine.

using System;
using System.Windows;
using System.Speech.Synthesis;
using System.Speech.Recognition;
using System.Net.Sockets;
Comments