Shop OBEX P1 Docs P2 Docs Learn Events
How to output keystrokes to my computer — Parallax Forums

How to output keystrokes to my computer

ECSimsECSims Posts: 19
edited 2008-01-07 07:05 in BASIC Stamp
Does anyone know how to output keystroke characters to my desktop computer with a BS2?· For example, when· I press a button on my BS2 project board, I would like to see a character show up on Notepad or Wordpad (or any other program for that matter).·· I have a USB2SER adapter on my BS2.··

I am building a flight simulator, and MS Flight Simulator will allow for keypress assignments within the program.· I would like to avoid adding extra cabling to my BS2 project board, like a PS2.· The idea solution would output the text through the USB cable that is all ready connected to the computer.·· Can this program code reside completely in the BS2, and not have to run external software loaded my desktop computer?

Thanks for your help.

Comments

  • SlashWeepSlashWeep Posts: 2
    edited 2008-01-03 23:47
    o0o0o0o0o0 sounds fun, my "hack" way would be to write a small simple c# that reads the debug from the BS2 then triggers win api for the key. There is some sample code to detect a push button switch in the book that you could use. But if your going with a flight sim I am pretty sure you will not get the performance you want, the trouble is (and I can be wrong) is the way you would need to detect the key press (you could miss the key press depending how you right the code). (hope I can post a link to another chip with out it getting deleted) http://www.ultimarc.com/ipac1.html I would get something like this, several companies make boards like this designed for home brew usb joystick controllers(you could even use the BS2 to trigger the other board). If you go with the BS2 I can write some sample c# if you have a compiler to react to the BS2 Debug Text.
  • Dennis FerronDennis Ferron Posts: 480
    edited 2008-01-04 00:17
    You're not going to be able to make the Basic Stamp on USB look like a USB keyboard because it identifies itself on the USB line as a serial device and not a Human Interface Device or keyboard, and you don't actually have direct access to the USB stuff to change that. Personally I would go ahead and wire a PS/2 cable to it. Or cut up an old keyboard and use the Basic Stamp to spoof a key press and let the keyboard chip handle the encoding.

    SlashWeep's method is the only way you are going to get it to work with just the USB. In C# and VB.NET, you have a function called "Sendkeys" that will take whatever key you feed it and send it to the open application. I don't think there's too much danger of missing the key press as long as the C# program is listening for the character in a loop. It would look something like this (pseudocode):

    serial_port = Serial.open("com3"); // or com1, or 2, or 4...

    while (running)
    {

    key_pressed = serial_port.receive();
    SendKeys(key_pressed);

    }
  • ECSimsECSims Posts: 19
    edited 2008-01-04 01:29
    Thanks for the great suggestions.· I should be able to handle the VB code with the Sendkeys command.· This seems like the most elegant solution, because it avoids additional hardware and cables.

    Have a great day!
  • SlashWeepSlashWeep Posts: 2
    edited 2008-01-04 03:36
    I was bored installing linux on my spare laptop, here is code to read the buffer. You will still need the parse the text you get back to see what key to send. Example of SendKeys can be found at http://msdn2.microsoft.com/en-us/library/ms171548.aspx·Problem with SendKeys is well you need to find the window you want to send the key to.
    [color=#0000ff]using[/color][color=#000000] System;[/color]
    
    [color=#0000ff]using[/color][color=#000000] System.Collections.Generic;[/color]
    
    [color=#0000ff]using[/color][color=#000000] System.ComponentModel;[/color]
    
    [color=#0000ff]using[/color][color=#000000] System.Data;[/color]
    
    [color=#0000ff]using[/color][color=#000000] System.Drawing;[/color]
    
    [color=#0000ff]using[/color][color=#000000] System.Text;[/color]
    
    [color=#0000ff]using[/color][color=#000000] System.Windows.Forms;[/color]
    
    [color=#008000]//Need to access the port
    
    [/color][color=#0000ff]using[/color][color=#000000] System.IO.Ports;[/color]
    
    [color=#0000ff]namespace[/color][color=#000000] COMBS2Test[/color]
    
    {
    
    [color=#0000ff]public[/color] [color=#0000ff]partial[/color] [color=#0000ff]class[/color] [color=#008080]frmMain[/color] : [color=#008080]Form
    
    [/color]{
    
    [color=#0000ff]public[/color] frmMain()
    
    {
    
    InitializeComponent();
    
    }
    
    [color=#008000]//Make sure you add a textbox called txtDebug to frmMain
    
    [/color][color=#008000]//Create a serial port
    
    [/color][color=#008080]SerialPort[/color] sPort = [color=#0000ff]new[/color] [color=#008080]SerialPort[/color]();
    
    [color=#0000ff]private[/color] [color=#0000ff]void[/color] frmMain_Load([color=#0000ff]object[/color] sender, [color=#008080]EventArgs[/color] e)
    
    {
    
    [color=#008000]//Change this to match your com port name
    
    [/color]sPort.PortName = [color=#800000]"COM4"[/color];
    
    [color=#008000]//Add an event for DataReceived
    
    [/color]sPort.DataReceived += [color=#0000ff]new[/color] [color=#008080]SerialDataReceivedEventHandler[/color](sPort_DataReceived);
    
    [color=#008000]//Open the port, not sPort.Close() will close it
    
    [/color][color=#008000]//Note you can not run the Basic Stamp Editor Debugger at the same time
    
    [/color]sPort.Open();
    
    }[color=#008000]//end function
    
    [/color][color=#008000]//When the event fires this function is called on a new thread
    
    [/color][color=#0000ff]private[/color] [color=#0000ff]void[/color] sPort_DataReceived([color=#0000ff]object[/color] sender, [color=#008080]SerialDataReceivedEventArgs[/color] e)
    
    {
    
    [color=#008000]//We need to call a Delegate to be thread safe
    
    [/color][color=#0000ff]this[/color].PostData(sPort.ReadExisting());
    
    }[color=#008000]//end function
    
    [/color][color=#008000]//Delegate type function
    
    [/color][color=#0000ff]private[/color] [color=#0000ff]void[/color] PostData([color=#0000ff]string[/color] Data)
    
    {
    
    [color=#008000]//Check to see if we are on the "Main" thread and not a spawned thread (event fire)
    
    [/color][color=#0000ff]if[/color] ([color=#0000ff]this[/color].InvokeRequired)
    
    {
    
    [color=#008000]//This is a short simple line that creates a new delegate that points to our self and then invokes it onto the main thread
    
    [/color][color=#0000ff]this[/color].EndInvoke([color=#0000ff]this[/color].BeginInvoke([color=#0000ff]new[/color] [color=#008080]MethodInvoker[/color]([color=#0000ff]delegate[/color]() { PostData(Data); })));
    
    }
    
    [color=#0000ff]else
    
    [/color]{
    
    [color=#008000]//This was called by the main thread so, now we can append the data to the text box
    
    [/color]txtDebug.Text += Data;
    
    }[color=#008000]//End if
    
    [/color]}[color=#008000]//End function
    
     
    
    [/color]}[color=#008000]//end class
    
    [/color]}[color=#008000]//end namespace
    
    [/color]
    

    ·
  • QuattroRS4QuattroRS4 Posts: 916
    edited 2008-01-04 12:40
    ECSims - I wrote an application many moons ago to act as a serial keyboard wedge ... any serial data received on a specific port will be injected to the keyboard buffer ... acts as though it was typed on the PC keyboard ...

    I attached it to this thread a long while back .. Should do the trick

    http://forums.parallax.com/showthread.php?p=627711

    it is listed as Par_kb.zip

    I have attached a link to it here also..
    http://forums.parallax.com/attachment.php?attachmentid=45863


    John Twomey

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'Necessity is the mother of invention'
  • ECSimsECSims Posts: 19
    edited 2008-01-07 07:05
    QuattroRS4 and Slashweep,

    Thanks for the code and your suggestions.· I have not had a chance to try out the program yet, but I'll let you know if I have problems.
Sign In or Register to comment.