Keyboard Wedge Does Not Work If Twedge Runs as Service

Problem: Services are running as a separate process without having a user session attached, so Windows cannot identify where to send the key strokes.

Solution: You can try if the option “Allow service to interact with desktop” makes a difference but usually you cannot use the key stroke simulation if TWedge runs as service.
Workaround: Write to a CSV file (see action templates). The file name could be based on the actual date so you get a new file each day (needs a small modification in the Script).
CSV files can be opened in Excel as well.

COM Ports on Citrix/Terminal Server

Problem: TWedge connects to the server, not to the client

Solution: You need to assign (or route) the COM port on the client to a virtual COM port on the server. This is called COM port redirection.

How to Send Data to a Device

TWedge V2

Solution: TWedge provides the function WriteToDevice () for this purpose. WriteToDevice () accepts two parameters:

  • The first one specifies the data which should be sent to the device.
  • The second parameter specifies the timeout (in milliseconds) for this operation.

The following example sends two lines of text: The first line prompts the device for accepting the username, the second line sends the username. Each line is terminated by a newline character (\n line feed):

WriteToDevice ("user\n", 1000);
WriteToDevice ("sysadmin\n", 1000);

TWedge V3

Solution: For this purpose TWedge provides the Javascript member function WriteData (). This functon exists in two variants with different parameters.:

  • Integer WriteData (Array arrData, Integer nTimeout)
  • Integer WriteData (String sData, Integer nTimeout, String sEncoding = "Latin1")

The following example sends data one time as binary array and one time as string. Inside of a string you can encode a line feed with the escape sequence \n:

var arrSend = [0x48, 0x65, 0x6c, 0x6c, 0x6f]; 
hConn.WriteData (arrSend, 200);
hConn.WriteData ("Hello\n", 1000);
hConn is the actual connection object.

Why Are All Commands Ignored by the Device

Solution: When using the function WriteToDevice () (or WriteData ()) to send device configuration or initiation commands, please check if a newline or carriage return character is required. This is especially important if the device uses a protocol like TELNET. A newline character is specified by \n.

The following example sends the text "user" followed by a newline to the device:

// TWedge 2:
WriteToDevice ("user\n", 1000);

// TWedge 3:
hConn.WriteData ("user\n", 1000);

Here is a list of common escape sequences:

  • \' Single quotation mark.
  • \" Double quotation mark. Use this escape sequence if you need to specify a " as part of a string.
  • \\ A single backslash character.
  • \b Backspace.
  • \n New-line (line-feed).
  • \r Carriage return. \r in combination with \n is used in DOS systems as sequence for a carriage return and new line. Most likely \n can be used instead.
  • \t Horizontal tab.
  • \f Form feed (for printers).

How Can I Append a Carriage Return to my Data?

You have to modify the script in the OnData or processData event code. Carriage Return can be added via the escape sequence \r (Carriage Return) or \n (Line Feed). In Javascript string concatenation is done with the + operator.

Menu (V2) Configuration - Script Editor - Edit Code: OnData

Menu (V3) Device - Configure - Advanced - Script Editor - Edit Code: onData

Change the following code line and add the + "\n" as shown below:

// V2 (OnData function):
SendKeyStrokes(DATA.replace (/\x00/g, " ") + "\n");

// V3 (processData function):
SendKeyStrokes (sData.replace(/\x00/g, " ") + "{ENTER}")

See the FAQ above for a list of common escape sequences.

Javascript Commands for Common Operations (processData)

Menu (TWedge V3) Device - Configure - Advanced - Script Editor - Edit Code in "processData"

Remove Carrige Return / Line Feed

  sData = sData.replace(/\x0D\x0A/g, "");  // remove CR/LF

Remove leading whitespace

  sData = sData.replace(/^\s+/, "");       // trim leading whitespace

Replace characters (via Hex Code)

  sData = sData.replace(/\x2E/g, ",");     // replace . => ,

Remove character sequences

  sData = sData.replace(/ d/, "");         // remove " d"

Parse a single value from a CSV sequence

  // sample data: 200,    177.50,mm   ,01
  if (sData.indexOf(',') > 0)
    sData = sData.split(',');
    sData = sData[1];  // take 2nd value
    sData = sData.replace(/^\s+/, "");       // trim leading whitespace
  // result will be 177.50

Add a time delay

  function onHotkey ()
    hInstance.Sleep (300); // wait 0.3 secs

How Can I Convert a Decimal (Float) to Integer?

My device sends 123.4 but I need only 123

Modify the script in the OnData event code (Ctrl - E). Replace the code lines with the following:

TWedge V2

var myData = Math.round(DATA).toFixed();

TWedge V3

var myData = DataToString(data);
myData = Math.round(myData).toFixed();

This code will perform rounding. If you don't need rounding, replace the Math.round with Math.floor.

My balance sends grams but I need kilograms

Change your OnData event code to the following:

TWedge V2

var myData = (parseInt(DATA)/1000).toFixed(3);

TWedge V3

var myData = DataToString(data);
myData = (parseInt(myData)/1000).toFixed(3);

How to Install TWedge 2.x as Windows Service?

We recommend upgrading to TWedge V3 which already includes a system service. If that is not possible, you can proceed as described below.
  1. Install the Windows Resource Toolkit. You can download it from Microsoft's Website.
  2. Open the Command Line (Start -> Run -> “cmd”), go to the installation directory of the Windows Resource Toolkit and enter the following command:
    Instsrv TWedge "C:\Program Files (x86)\Windows Resource Kits\Tools\srvany.exe".
    Command Line: Installation of Windows Resource Toolkit
  3. Open the registry editor (Start -> Run -> “regedit”) and look for the following registry key: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\TWedge.
    Registry Editor: TWedge Registry Key
  4. Now add a "Parameters" key with a value called "Application". It contains the path to the executable of TWedge (default: C:\Program Files (x86)\TEC-IT\TWedge2\Bin\TWedge.exe C:\TWedge.twi). The twedge.twi file opens TWedge with the predefined settings!
  5. Registry Editor: Add new key Parameters
  6. Open the properties of the new TWedge service which you can find in the Control Panel -> Administrative Tools -> Services.
  7. Go to the “Log On” tab and make assure that the checkbox “Allow service to Interact with desktop” is activated.
    Services:  TWedge Properties
  8. Run the TWedge service.