# Have Robot type any arbitrary message via keyboard in vision?

**URL:** <https://forum.inductiveautomation.com/t/have-robot-type-any-arbitrary-message-via-keyboard-in-vision/47190>\
**Category:** Ignition\
**Tags:** ignition79\
**Created:** [May 27, 2021, 2:36pm UTC](https://forum.inductiveautomation.com/t/have-robot-type-any-arbitrary-message-via-keyboard-in-vision/47190 "2021-05-27T14:36:08Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [May 27, 2021, 2:36pm UTC](https://forum.inductiveautomation.com/t/have-robot-type-any-arbitrary-message-via-keyboard-in-vision/47190/1 "2021-05-27T14:36:08Z")

</div>

I’m writing end to end tests and am using `java.awt.Robot` to simulate a user. I can’t figure out how to have my robot type in any message I want it to. From the documentation I have to feed it either an KeyEvent.VK\_Something, or just an integer, but how can I do that on on the fly for any string of characters? I figure I’ll need to iterate through the message

```auto
def type_on_keyboard(message):
	"""
	message: str, the key strokes you want simulated
	"""
    key_presses = [convert(x) for x in message]
    for key_to_press in key_presses:	
        robot.keyPress(key_to_press)
    	robot.keyRelease(key_to_press)

```

But I don’t know what the `convert` function should look like. Do I have to parse it and convert each character to `VK_A, VK_B`, etc or is there a better way?

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [May 27, 2021, 2:37pm UTC](https://forum.inductiveautomation.com/t/have-robot-type-any-arbitrary-message-via-keyboard-in-vision/47190/2 "2021-05-27T14:37:34Z")

</div>

[https://docs.oracle.com/javase/7/docs/api/constant-values.html#java.awt.event.KeyEvent.VK\_A](https://docs.oracle.com/javase/7/docs/api/constant-values.html#java.awt.event.KeyEvent.VK_A)

VK\_A through VK\_Z are the same as their ascii equivalent.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [May 27, 2021, 2:39pm UTC](https://forum.inductiveautomation.com/t/have-robot-type-any-arbitrary-message-via-keyboard-in-vision/47190/3 "2021-05-27T14:39:16Z")

</div>

Do you need to actually type for the purposes of your test, or could you put the ‘message’ into the clipboard buffer and simulate a paste?

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [May 27, 2021, 2:39pm UTC](https://forum.inductiveautomation.com/t/have-robot-type-any-arbitrary-message-via-keyboard-in-vision/47190/4 "2021-05-27T14:39:59Z")

</div>

This (`getExtendedKeyCodeForChar`) may be useful as well: [KeyEvent (Java SE 9 & JDK 9 )](https://docs.oracle.com/javase/9/docs/api/java/awt/event/KeyEvent.html#getExtendedKeyCodeForChar-int-)

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [May 27, 2021, 2:41pm UTC](https://forum.inductiveautomation.com/t/have-robot-type-any-arbitrary-message-via-keyboard-in-vision/47190/5 "2021-05-27T14:41:50Z")

</div>

I don’t think actual typing is required if you now how to do this with a clipboard copy and paste?

@Kevin.Herron So I could use the getExtendedKeyCodeForChar on each char in my message and that would be the key to be pressed and released?

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [May 27, 2021, 2:44pm UTC](https://forum.inductiveautomation.com/t/have-robot-type-any-arbitrary-message-via-keyboard-in-vision/47190/6 "2021-05-27T14:44:00Z")

</div>

I don’t know, I’m just looking at the docs, try it and see.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [May 27, 2021, 2:46pm UTC](https://forum.inductiveautomation.com/t/have-robot-type-any-arbitrary-message-via-keyboard-in-vision/47190/7 "2021-05-27T14:46:32Z")

</div>

Paste method would be something like:

```auto
from java.awt.datatransfer import StringSelection
from com.inductiveautomation.ignition.client.util import ClipboardUtil

def toClipboard(message):
	ClipboardUtil.doCopyTransferableToClipboard(StringSelection(message))

```

And then simulate a Ctrl + V keypress.

> <https://stackoverflow.com/questions/1248510/convert-string-to-keyevents/11779211#11779211>

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [May 27, 2021, 2:49pm UTC](https://forum.inductiveautomation.com/t/have-robot-type-any-arbitrary-message-via-keyboard-in-vision/47190/8 "2021-05-27T14:49:50Z")

</div>

Keycode method might be something like this:

```auto
from java.awt.event import KeyEvent

message = u"hello, world"
codes = [KeyEvent.getExtendedKeyCodeForChar(ord(c)) for c in message]

for code in codes:
	print code

```

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [May 27, 2021, 2:58pm UTC](https://forum.inductiveautomation.com/t/have-robot-type-any-arbitrary-message-via-keyboard-in-vision/47190/9 "2021-05-27T14:58:26Z")

</div>

Oh I never seen the `ord` function before. That was the missing piece of the puzzle for me. Thanks.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [May 27, 2021, 2:59pm UTC](https://forum.inductiveautomation.com/t/have-robot-type-any-arbitrary-message-via-keyboard-in-vision/47190/10 "2021-05-27T14:59:50Z")

</div>

`ord` may or may not behave weirdly if you ever pass unicode in, I don’t know whether Jython sticks to what Java would do or what Python would do, and character encoding is a total mess anyways…so here’s an alternate way to get the same info that’s less likely to have weird edge cases, I think:

```auto
from java.awt.event import KeyEvent
from java.lang import String

message = String(u"hello, world")
codes = [KeyEvent.getExtendedKeyCodeForChar(k) for k in message.chars().iterator()]

for code in codes:
	print code

```

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [May 27, 2021, 3:09pm UTC](https://forum.inductiveautomation.com/t/have-robot-type-any-arbitrary-message-via-keyboard-in-vision/47190/11 "2021-05-27T15:09:35Z")

</div>

Awesome this works nicely. If I can bother you about one last thing regarding this, I am trying to put a tiny pause between pressing the keys to make it look like a real user. I currently have this

```auto
def small_delay():
	def foo():
		pass
	system.util.invokeLater(foo, 500)

def type_on_keyboard(message):
	"""
	message: str, the key strokes you want simulated
	"""
	str_message = String(message)
	key_presses = [KeyEvent.getExtendedKeyCodeForChar(k) for k in str_message.chars().iterator()]
	for key_to_press in key_presses:
		rob.keyPress(key_to_press)
		small_delay()
		rob.keyRelease(key_to_press)

```

But it’s still instantaneous, and I realize that my `small_delay` won’t work the way I have it, but I don’t see how I can do this without time.sleep, which I know is to be avoided. Any ideas?

Edit: Think I got it like this in case anyone else needs this:

```auto
def type_on_keyboard(message):
	"""
	message: str, the key strokes you want simulated
	"""
	str_message = String(message)
	key_presses = [KeyEvent.getExtendedKeyCodeForChar(k) for k in str_message.chars().iterator()]
	for num, key_to_press in enumerate(key_presses):
		delay = (num+1) * 200
		def typeKey(key=key_to_press):
			rob.keyPress(key)
			rob.keyRelease(key)
		system.util.invokeLater(typeKey, delay)

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [May 27, 2021, 4:02pm UTC](https://forum.inductiveautomation.com/t/have-robot-type-any-arbitrary-message-via-keyboard-in-vision/47190/12 "2021-05-27T16:02:36Z")

</div>

`invokeLater` calls will already be serial/in order, you should just be able to put them on the queue:

```auto
def type_on_keyboard(message):
	"""
	message: str, the key strokes you want simulated
	"""
	str_message = String(message)
	
	def typeKey(key):
		rob.keyPress(key)
		rob.keyRelease(key)

	for key_to_press in (KeyEvent.getExtendedKeyCodeForChar(k) for k in str_message.chars().iterator()):
		system.util.invokeLater(lambda: typeKey(key_to_press))

```

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [May 27, 2021, 4:19pm UTC](https://forum.inductiveautomation.com/t/have-robot-type-any-arbitrary-message-via-keyboard-in-vision/47190/13 "2021-05-27T16:19:09Z")

</div>

I do like how clean it is. After getting the typing to work, I realized all my robot instructions had to be utilizing system.util.invokeLater otherwise while I was still logging on, the mouse would start moving due to the next function call. I chose the convention that each function returns the ms needed for the next call of system.util.invokeLater, and have a constant `DELAY_FACTOR` to easily shorten or lengthen time between actions. My setup is like this -

```auto
DELAY_FACTOR = 200

WINDOW_SELECT_PIXELS = {
	"Privileged Access": (1300, 1050),
	"Overview": (500, 1050),
	"Print Screen": (1900,1050),
	"Login": (1400,40)
}

def goToWindow(window, initial_delay=0):
	"""
	Goes to a specific window via bottom navigation.
	Args:
		window: str, which window do you want to go to?
	Returns:
		None, moves mouse and clicks button, or throws KeyError if bad window name
	"""
	x, y = WINDOW_SELECT_PIXELS[window]
	def leftClickForWindow(x=x, y=y):
		leftClick(x, y)
	system.util.invokeLater(leftClickForWindow, initial_delay)
	return initial_delay+DELAY_FACTOR

def pressTab(initial_delay=0):
	def pressVKTab():
		rob.keyPress(KeyEvent.VK_TAB)
		rob.keyRelease(KeyEvent.VK_TAB)
	system.util.invokeLater(pressVKTab, initial_delay)
	return initial_delay+DELAY_FACTOR

def pressEnter(initial_delay=0):
	def pressVKEnter():
		rob.keyPress(KeyEvent.VK_ENTER)
		rob.keyRelease(KeyEvent.VK_ENTER)
	system.util.invokeLater(pressVKEnter, initial_delay)
	return initial_delay+DELAY_FACTOR

def type_on_keyboard(message, initial_delay=0):
	"""
	message: str, the key strokes you want simulated
	initial_delay: int, - for the first thing you type in, but probably otherwise. needed for chaining
	"""
	str_message = String(message)
	key_presses = [KeyEvent.getExtendedKeyCodeForChar(k) for k in str_message.chars().iterator()]
	maxDelay = None
	for num, key_to_press in enumerate(key_presses):
		delay = ((num+1) * DELAY_FACTOR) + initial_delay
		maxDelay = delay
		def typeKey(key=key_to_press):
			rob.keyPress(key)
			rob.keyRelease(key)
		system.util.invokeLater(typeKey, delay)
	return maxDelay + DELAY_FACTOR

def moveMouse(x, y, initial_delay=0):
	"""
	Call leftClick at a specified pixel.
	"""
	def moveMouseDelay(x=x, y=y):
		rob.mouseMove(x, y)
	system.util.invokeLater(moveMouseDelay, initial_delay)
	return initial_delay+DELAY_FACTOR

def loginAsMe(initial_delay=0):
	wait_0 = goToWindow("Login", initial_delay=initial_delay)
	wait_1 = type_on_keyboard("secretusername") 
	wait_2 = pressTab(initial_delay=wait_1) 
	wait_3 = type_on_keyboard("secretpassword", initial_delay=wait_2)
	wait_4 = pressEnter(initial_delay=wait_3)
	return wait_4

```

This works for me. It looks like a realistic user at 200 ms delay. I wish my `loginAsMe` function looked nicer, as I’m always getting the result of the previous function and feeding it to the next `initial_delay`, but it’s readable enough and works. Just posting this here for comments/concerns/posterity, as I haven’t seen too many posts on trying to write end to end tests with Robot.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [May 27, 2021, 5:01pm UTC](https://forum.inductiveautomation.com/t/have-robot-type-any-arbitrary-message-via-keyboard-in-vision/47190/14 "2021-05-27T17:01:44Z")

</div>

I wonder if something like this would work? You’ll have to invoke `loginAsMe` from an async thread - EventQueue will throw if you try to call `invokeAndWait` from the EDT.

```auto
from java.awt import EventQueue

DELAY_FACTOR = 200

WINDOW_SELECT_PIXELS = {
	"Privileged Access": (1300, 1050),
	"Overview": (500, 1050),
	"Print Screen": (1900,1050),
	"Login": (1400,40)
}

def __invokeBlocking(fn, *args):
	EventQueue.invokeAndWait(lambda: fn(*args))

def __pressKey(keyCode):
	rob.keyPress(keyCode)
	rob.keyRelease(keyCode)

def goToWindow(window, initial_delay=0):
	"""
	Goes to a specific window via bottom navigation.
	Args:
		window: str, which window do you want to go to?
	Returns:
		None, moves mouse and clicks button, or throws KeyError if bad window name
	"""
	x, y = WINDOW_SELECT_PIXELS[window]
	__invokeBlocking(leftClick, x, y)

def pressTab():
	__invokeBlocking(__pressKey, KeyEvent.VK_TAB)

def pressEnter():
	__invokeBlocking(__pressKey, KeyEvent.VK_ENTER)

def type_on_keyboard(message, initial_delay=0):
	"""
	message: str, the key strokes you want simulated
	initial_delay: int, - for the first thing you type in, but probably otherwise. needed for chaining
	"""
	str_message = String(message)
	for keyCode in (KeyEvent.getExtendedKeyCodeForChar(k) for k in str_message.chars().iterator()):
		__invokeBlocking(__pressKey, keyCode)
	
def moveMouse(x, y):
	"""
	Call leftClick at a specified pixel.
	"""
	__invokeBlocking(rob.mouseMove, x, y)

def loginAsMe():
	goToWindow("Login")
	type_on_keyboard("secretusername") 
	pressTab() 
	type_on_keyboard("secretpassword")
	pressEnter()

```

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [May 27, 2021, 6:13pm UTC](https://forum.inductiveautomation.com/t/have-robot-type-any-arbitrary-message-via-keyboard-in-vision/47190/15 "2021-05-27T18:13:53Z")

</div>

That looks much cleaner, I will try it tomorrow. One weird thing is I can’t seem to get it this to work for undrescores unfortunately. Right now this keeps throwing me an error that it’s an invalid key code, even when I use the KeyEvent.VK\_UNDERSCORE. I thought I maybe needed a caveat for special characters and made a dictionary map from numbers to KeyEvent.VK\_UNDERSCORE, but that’s the same exact key code the getCharCode is giving in the first place. Here’s a script you can run in the script console and replicate the problem. Not sure what is going wrong.

```auto
import java.awt.Robot
from java.awt.event import InputEvent, KeyEvent
from java.lang import String
import java.lang.IllegalArgumentException

ROBOT_DELAY_MS = 100
DELAY_FACTOR = 200

SPECIAL_CHARACTERS = {
	523: KeyEvent.VK_UNDERSCORE 
}

rob = java.awt.Robot()
rob.delay(ROBOT_DELAY_MS)

def getKeyCode(char):
	return KeyEvent.getExtendedKeyCodeForChar(char)

def type_on_keyboard(message, initial_delay=0):
	"""
	message: str, the key strokes you want simulated
	initial_delay: int, - for the first thing you type in, but probably otherwise. needed for chaining
	"""
	str_message = String(message)
	print "working on %s"%(str(str_message))
	key_presses = [getKeyCode(k) for k in str_message.chars().iterator()]
	maxDelay = None
	for num, key_to_press in enumerate(key_presses):
		delay = ((num+1) * DELAY_FACTOR) + initial_delay
		maxDelay = delay
		def typeKey(key=key_to_press):
			print "about to press %s"%(str(key))
			try:
				rob.keyPress(key)
				rob.keyRelease(key)
			except java.lang.IllegalArgumentException, e:
				# Not try/excepting keyerror here as if we don't have it, we probably want to alert the dev so they add it
				special_key = SPECIAL_CHARACTERS[key]
				print "special_key:" + str(special_key)
				rob.keyPress(special_key)
				rob.keyRelease(special_key)
		system.util.invokeLater(typeKey, delay)
	return maxDelay + DELAY_FACTOR

type_on_keyboard("some_cool")

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [May 28, 2021, 3:39pm UTC](https://forum.inductiveautomation.com/t/have-robot-type-any-arbitrary-message-via-keyboard-in-vision/47190/16 "2021-05-28T15:39:18Z")

</div>

Extended characters that don’t have an actual mapping on the current keyboard layout will throw that error. You’ll have to simulate it via a `Shift + -` ‘stack’ of key presses:

> <https://stackoverflow.com/questions/59179186/java-how-to-write-with-robot-class/59179581#59179581>
