Main Site Documentation

Emulate USB keyboard and mouse simultaneously


#1

I have a Domino 1.1 board. I’m newby. I created a keyboard emulation what works perfect.
Is it possible to to emulate a mouse and a keyboard simultaneously?

Thanks for your replies in advance.


#2

Yes, domino supports usb hubs.
Connect both of them to a usb hub.
Please use a powered usb hub (one with a external wall wart).


#3

Full source code is:


using System;
using System.Threading;

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.USBClient;
using GHIElectronics.NETMF.Hardware;
using GHIElectronics.NETMF.IO;

namespace FEZ_Domino_Keyboard_EMU
{
    public class Program
    {
        static int max = 6;
        static long[] intervals = { 300, 25, 5, 45, 30, 16 };
        static DateTime[] stopwatch;
        static USBC_Key[][] keypresses = new USBC_Key[6][] { new USBC_Key[2] { USBC_Key.F10, USBC_Key.F9 }, 
                                                             new USBC_Key[1] { USBC_Key.D9 },
                                                             new USBC_Key[2] { USBC_Key.X, USBC_Key.Z },
                                                             new USBC_Key[1] { USBC_Key.F7 },
                                                             new USBC_Key[1] { USBC_Key.F8 },
                                                             new USBC_Key[3] { USBC_Key.F6, USBC_Key.N, USBC_Key.F6 },
        };
        static int[] delays = { 2000, 1000, 1000, 1000, 1000, 1500 };
        static USBC_Keyboard kb;
        static bool onledstate = false;
        static long pm;
        static OutputPort led = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di12, false);
        static OutputCompare oc = new OutputCompare((Cpu.Pin)FEZ_Pin.Digital.Di13, false, 360);
        static InputPort Button = new InputPort((Cpu.Pin)FEZ_Pin.Digital.Di8, false, Port.ResistorMode.PullUp);
        static bool DebugMode = false;
        static PersistentStorage sd;

        private static void BlinkOnLED(Object o)
        {
            led.Write(onledstate);
            onledstate = !onledstate;
        }

        public static void Main()
        {
            uint[] Buffer = new uint[360];//Create a buffer for 180 on times and 180 off times
            for (int i = 0; i < 360; i = i + 2) //Fill the buffer with the on times from the SIN function.
            {
                int SinValue = (Microsoft.SPOT.Math.Sin(i) + 1000) * 10 ;//Value will vary between 0 and 20000

                Buffer[i] = (uint)(SinValue);//On time
                Buffer[i + 1] = 20000 - Buffer[i]; //Off time is the remaining time, of 20000us, after On time.
            }
            oc.Set(false, Buffer, 0, Buffer.Length, true);//Start the Output Compare, repeating.
            stopwatch = new DateTime[max];
            for (int i = 0; i < 3; i++) stopwatch[i] = DateTime.Now;
            TimeSpan elapsed;
            Random r = new Random();
            pm = r.Next(5);
            // Check debug interface
            if (Configuration.DebugInterface.GetCurrent() == Configuration.DebugInterface.Port.USB1) DebugMode = true;
            else
            {
                if (Button.Read())
                {
                    // Mass Storage Mode
                    led.Write(true);
                    USBC_MassStorage ms = USBClientController.StandardDevices.StartMassStorage();
                    while (true)
                    {
                        try
                        {
                            sd = new PersistentStorage("SD");
                            break;
                        }
                        catch
                        {
                            //SD card not detected, wait for a second and try again
                            Thread.Sleep(1000);
                        }
                    }
                    ms.AttachLun(0, sd, " ", " ");
                    // enable host access 
                    ms.EnableLun(0);
                    Thread.Sleep(Timeout.Infinite);
                }
                else kb = USBClientController.StandardDevices.StartKeyboard(); // Start keyboard
            }
            while (true)
            {
                Thread.Sleep(500);
                onledstate = !onledstate;
                led.Write(onledstate);
                pm = r.Next(5);
                for (int i = 0; i < max; i++)
                {
                    elapsed = DateTime.Now - stopwatch[i];
                    if (elapsed.Ticks >= (pm + intervals[i]) * 10000000)
                    {
                        stopwatch[i] = DateTime.Now;
                        if (DebugMode)
                        {
                            using (new Timer(BlinkOnLED, null, 0, 50))
                            {
                                foreach (USBC_Key k in keypresses[i])
                                {
                                    Debug.Print(k.ToString());
                                }
                                Thread.Sleep(delays[i]);
                            }
                            led.Write(false);
                        }
                        else
                        {
                            if (USBClientController.GetState() == USBClientController.State.Running)
                            {
                                using (new Timer(BlinkOnLED, null, 0, 50))
                                {
                                    foreach (USBC_Key k in keypresses[i])
                                    {
                                        kb.KeyTap(k);
                                        Thread.Sleep(delays[i]);
                                    }
                                }
                                led.Write(false);
                            }
                        }
                    }
                }
            }
        }
    }
}


#4

My problem is that I cannot simulate a mouse click without a USB mouse emulation. Any other idea is welcome how to perform it :slight_smile:


#5

Why would you want to emulate a mouse click when you first asked how to connect a mouse and keyboard? :stuck_out_tongue:


#6

Sorry, it’s my fault. My question is about emulating a keyboard and a mouse in the same time. My English is bad :-[

I searched the web and found that a composite USB device is the proper way to implement this because I don’t want to stop the keyboard.