Hardware Macro functionality?

Status
Not open for further replies.

Xennon

Member
Hi all.

I've only just joined up to the forum in the hope of getting some advice/guidance about a project that suddenly popped into my head.

The idea:
Use something like the Teensy to send keyboard macro key/character sequences to the PC (Windows) so that the receiving program "believes" they are coming from an actual physical keyboard.

Purpose:

I have noticed that many games (and other software) seem to be offering little or no keyboard re-mapping functionality or macro support and in fact go to extreme lengths to ensure these features are strictly monitored/controlled. I find this frustrating because I rarely like the key-binds that are force fed to me by the developers of these app. I have (in the past) managed to circumvent most of these restrictions by using utilities like AutoHotKey but nowadays it appears more and more devs are wise to this and actively preventing the use of such apps and in the case of games, banning anyone found to be using such software. To be clear, my intent is NOT to do any kind of cheating or botting in games - I just want to use the keys I want, how I want.

That's what made me think that maybe if I could use a piece of hardware like the Teensy, I could "fool" these uncooperative apps into using my preferred key-binds and macros.

Question:
Can someone offer any advice and/or guidance as to how I might implement this idea? I have little to no knowledge of electronics but I think I manage a few lines of c/c++ code when the need arises. I've searched the forums and although I've not found an exact answer, everything "seems" to indicate that this might be do-able.

My idea was initially to plug my existing PS/2 keyboard into a "black box" which would process my keystrokes in accordance with some "rules" and then translate those keystrokes and on-forward them to the PC via a USB port - effectively converting my PS/2 KB into a USB KB but with this intermediate black box.

I know the Teensy has "some" onboard storage for programs etc, but I'm uncertain how much I can achieve with the seemingly small amount of RAM.

Anyway, to avoid rambling on for much longer, I might leave it here and see what responses I get. Any suggestions/advice will be appreciated, even if it involves other solutions might be out there that I'm not aware of.

Thx
Xen
 
Last edited:
It's totally doable and many folks have done it. It will require some skills and knowledge of electronics, programming and finding the best tutorial for you.

It's usually a good idea to figure out what software you need first and let the software choice guide your hardware choices, especially if you're not ready to write your own software.

Somehow you'll need to connect a PS/2 connector to your Teensy. One way is to get a PS/2 connector breakout board and solder it to your Teensy. Before you've figured out which wires to connect where, you might want to get some spring-loaded test leads.

You might also want to find a suitable project box. I've purchased a few from mouser.com. The key things to keep in mind are the interior dimensions and how you're planning to attach the stuff in the box to the box. One way is to get male pin headers and protoboard. You solder the headers to the Teensy (or purchase the teensy with pins already), solder the pins to the protoboard and screw the protoboard to the enclosure.

I haven't used the particular components below. Just trying to give you an idea of the kind of parts you'll need.

http://www.mouser.com/ProductDetail...kpid=2174263&gclid=CMji-tXJkscCFUVlfgodYR4Evw
http://www.adafruit.com/products/804
http://www.mouser.com/Enclosures/Enclosures-Boxes-Cases/_/N-5g3p

Regarding software, if you go the Teensy route, you'll need to install the Arduino software and then install the Teensyduino software, being sure to pick versions that are compatible.
 
Hi all.

I've only just joined up to the forum in the hope of getting some advice/guidance about a project that suddenly popped into my head.

The idea:
Use something like the Teensy to send keyboard macro key/character sequences to the PC (Windows) so that the receiving program "believes" they are coming from an actual physical keyboard.

Purpose:

I have noticed that many games (and other software) seem to be offering little or no keyboard re-mapping functionality or macro support and in fact go to extreme lengths to ensure these features are strictly monitored/controlled. I find this frustrating because I rarely like the key-binds that are force fed to me by the developers of these app. I have (in the past) managed to circumvent most of these restrictions by using utilities like AutoHotKey but nowadays it appears more and more devs are wise to this and actively preventing the use of such apps and in the case of games, banning anyone found to be using such software. To be clear, my intent is NOT to do any kind of cheating or botting in games - I just want to use the keys I want, how I want.

That's what made me think that maybe if I could use a piece of hardware like the Teensy, I could "fool" these uncooperative apps into using my preferred key-binds and macros.

Question:
Can someone offer any advice and/or guidance as to how I might implement this idea? I have little to no knowledge of electronics but I think I manage a few lines of c/c++ code when the need arises. I've searched the forums and although I've not found an exact answer, everything "seems" to indicate that this might be do-able.

My idea was initially to plug my existing PS/2 keyboard into a "black box" which would process my keystrokes in accordance with some "rules" and then translate those keystrokes and on-forward them to the PC via a USB port - effectively converting my PS/2 KB into a USB KB but with this intermediate black box.

I know the Teensy has "some" onboard storage for programs etc, but I'm uncertain how much I can achieve with the seemingly small amount of RAM.

Anyway, to avoid rambling on for much longer, I might leave it here and see what responses I get. Any suggestions/advice will be appreciated, even if it involves other solutions might be out there that I'm not aware of.

Thx
Xen

Hi Xen

This is definitely doable. The question you should answer is why you want to do it: To solve your "key mapping problem", or to learn about how to build something like this.

If it is the former, I suggest you check out some of the "high-end gamer keyboards" in the market. They offer very extensive keyboard macro functionality (implemented in the keyboard, not bypassed by software on the PC), and likely you can find something that fits your bill. They are not cheap - you should expect somewhere around USD 100 or a little more.

If it is the latter, this is certainly a nice project to learn, but do not expect it to be easy given what you say about your prior knowledge. First, you need to learn how to connect your PS/2 keyboard to a Teensy. Then you need to learn on how to write code for the Teensy (Arduino). Then you need to learn the "protocol" of you PS/2 keyboard (i.e. what signals go over the wires when a button is pressed on the keyboard), and you need to write code for the Teensy that checks the wires and converts that into the keys that have been pressed. Then you need to learn some things about USB, e.g. how to identify the sender in as a keyboard to the PC, and you need to write (or reuse from the USB libraries on Teensy) the code that correctly initializes everything and sends keystrokes via USB to the PC. And finally, you need to come up with some mapping logic for your macros, and implement that on the Teensy as well. As said, a very nice project to learn, but do not expect to get it done in a day or two (unless you are an expert, which I understand you are not).

The good thing is that Teensy 3 has an extensive USB library that will help you with that most difficult part (IMHO). It even has a "USB Keyboard" library, which helps you in doing part of the job.

You should not be concerned about memory for this. The program is stored in flash memory, and does not consume RAM. You are not going to store a lot of data - you have your keystrokes, that you check against a set of translation patterns, and then pass them on. So let us say you have 100 patterns, each of them being up to 20 strokes long, and that you need to store 2 bytes per keystroke (this is not just a character, but all auxiliary keys as well), you end up with 100*20"2 = 4000 bytes for the mapping table, and all the rest is small.

BTW, to be fair to the manufacturers of game software: They do not try to be "uncooperative", but they have have another parameter they optimize, namely latency. When you play games, you often want an immediate reaction when you press a key. The time it takes from a keyboard through USB to your PC is in the ballpark of 1 millisecond (in an ideal implementation - based on the design of USB), then it takes a few milliseconds to go through Windows (or whatever OS you use) until your game sees it. Putting a "macro translator" in between adds a couple of more milliseconds. It is this delay of altogether 10ms or so that action gamers hate, and that is why such games bypass as much software on the PC as possible and directly talk to the keyboard (bringing the latency down to a little more than 1ms). This is also why high-end gamer keyboards do the translation on the keyboard, effectively not adding any latency.

Good luck with whatever you are going to do!
CodingIsFun
 
Last edited:
Hi pictographer, thanks for the reply. I've seen a good example of how to connect the ps/2 keyboard to the teensy so that it effectively converts it into a USB keyboard - so that part seems easy enough and I have enough xp to manage the physical wiring aspect.

I'm unsure what you mean by "what software you need". The software that I need I will probably have to code myself and is the code I put inside the teensy to translate and act upon incoming keystrokes from the keyboard before sending them onto whatever active program is running on the PC. (ie my macro/translation code)

My concerns are 2 fold - (1) Can the teensy hold the code I need - I'm not sure how "big" it will end up. (2) I will need to re-flash the teensy every time I want to use a different set of macros that are specific to a different application. From what I've seen, re-flashing does not take long and if I'm really clever (and the teensy has the room for it) I could write my code in sub-sections where each one applies to a specific app and might be triggered via some form of keyboard key sequence so that only that part of the code become the active part until i switch to another.

I'm also a little confused as to which model of the teensy I should go for - 2.0, 2++, 3.1? The cost differences are not huge, so maybe the 3.1 might offer the best for any future projects given that it has the most memory available.

I'll continue to check out anything relevant and hope all the pieces fall into place eventually.
 
g'day codingisfun and thank you for your reply.

You raise a number of relevant and interesting points and to answer your question about my motives, it's basically "both" .. I want to learn about this kind of stuff as well as get something useful out of it in the end.

As for the high-end keyboards with built in macro features - yes, i have considered those but from what I've read, they require special software to run on the PC in order to use those macro features. Furthermore I've also seen reports of some developers specifically targeting these users either by detecting this required software running on the PC or by detecting the actual keyboard type/model/etc and not allowing them to be used. A little over the top IMO, but it takes all sorts I guess.

What you say about the translation table size makes a lot of sense, so it seems I shouldn't be too concerned about needing more coding space.

As for your latency point, the high-end keyboards (from my understanding) need software to be running on the PC to use those macro features, which would introduce the latency you describe - but perhaps I'm wrong and there are some really good ones that do not use that approach. I've not seen any that specify that although I've not really looked very hard either since the idea of DIY is a little more stimulating. Even if there are some that do it all on the keyboard, wouldn't my approach be more or less the same as theirs except my "code" is sitting inside an external case instead of tucked inside the keyboard?

The issues you raise regarding all the steps involved are a bit daunting, I admit, however there also seems to be a ton of "usable" pre-existing code (and information) out there in the public domain so I might be lucky enough to not have to (fully) re-invent the wheel - just work out how to make it "roll" differently. That's the hope at least.

Thanks again for your input.
 
This is how I conceptualise what I'me trying to do - if my explanation was unclear.

Option 1.jpg

If anyone has any ideas how I might change this of offer a better approach, please let me know.
 
Hi Xennon

Alright, if you want to learn how to do these kind of things, go ahead. I believe this is a nice project to get started on these matters.

Regarding gamer keyboards, I cannot tell about all of them and I am not an expert at all (I do not play games, but I work on a gamer keyboard as I just love the touch of their mechanical switches). I do believe, however, that most of them store the macros/mappings (or a copy of them) in the keyboard itself, translate them when you hit the key, and the PC does not even see what was happening, like you explain for your box in between. As an example, check out this keyboard review from PC magazine about a Corsair keyboard (http://www.pcmag.com/article2/0,2817,2398127,00.asp) - it says [note that this is not a recommendation for Corsair keyboards - I do not know their products, do not have one, and have no relationship to them at all - this is just an example]:

"Macros can be programmed without having to deal with any specialized software. Instead, you simply press the MR (Macro Record) button on the keyboard to begin recording a macro, and press it again to end recording. You can not only program any of the 18 macro buttons, you can do it again for three different sets of macros, selected with buttons M1, M2 and M3, which swaps out one bank of macros for another.

For those who want more granular control of their profiles and macros, you can download the accompanying Vengeance Gaming Software, which provides an on-screen macro editor. The software is compatible with Windows (Windows 7, Vista, and XP) but not Mac—sorry Apple fans. The keyboard is equipped with 36kb of built-in memory, saving your profiles and macros to the keyboard itself instead of the PC, letting you take your customizations with you, even when using the keyboard with a different PC. "


That sounds to me like everything is happening on the keyboard itself; for some configuration they have a PC program that lets you edit macros, but in the end, the macros are on the keyboard (otherwise you could not take them with you to another PC).

On your project, once you have the Teensy and the Arduino environment running, you should get some results quite quickly. As a first step, I would try to use the "USB Keyboard" examples on Teensy and let the Teensy operate as something that sends keystrokes to the PC. Once you have that up and running, it is time to figure out the PS/2 keyboard interface. A start would be wikipedia, such as https://en.wikipedia.org/wiki/PS/2_port. There you see that the interface is not that fast (10-16 kHz serial data), so easy to receive and decode with a Teensy. Connect the PS/2 plug data and clock lines to some general purpose Teensy pins, and use digitalRead() on Arduino to read them. Play around and see whether you can detect the individual key strokes on the keyboard. Once you have that, it is time to put the two pieces together and send the keys you detect from the keyboard to your PC. Once that is working, build whatever you want in terms of macro processing.

Good luck again!
CodingIsFun
 
The observation I have is that T_3.1 has a single USB port - and your drawing shows the black box with two USB's feeding through - so you'd need two Teensies that talked to each other (serially?) - maybe a 3.1 and an LC would work for that if you didn't use two 3.1's. If the first Teensy talked to the keyboard, your first step would be catching the keyboard output and echoing that to Teensy #2 and then T #2 to the computer. Then you could define a 'key sequence' to have T #1 program a Macro to assign a key. You could add a $9 PJRC display on the black box for helping with program mode and do it all from the keyboard.

I just got a pair of these lighted keyboards on discount for $30 with 320KB macro RAM - for 9 keys? - they have rubber key returns, not mechanical. They have a software app to set the Macros - maybe other ways to program - I got them and left town after I replaced my old keyboard as I wanted fresh keycaps and lighted keys.

GAMDIAS Ares GKB6010 Gaming Keyboard
 
Regarding "what software you need", I meant there are libraries and applications that help. There's software that can help you map out the connections inside common types of keyboards. This would allow you to insert the Teensy (or whatever) directly inside the keyboard in between the matrix of wires and the interface board that sends USB to the PC. There are libraries for the Teensy that help you send keystrokes to the PC over USB, as you know; and also libraries that interpret PS/2 data, if I recall correctly. Also, I'd bet that at least a few of the others who have done this sort of thing before have published their code on Github or Source Forge, etc. (be careful with Source Forge).
 
... and your drawing shows the black box with two USB's feeding through ...

Hi defragster, thanks for your input. Actually my drawing is "supposed" to indicate that the black box is receiving input from the ps/2 keyboard and the other node is the Teensy's own USB output jack (although my drawing shows a ps/2 connector instead of what it should be - a USB connector) - bad drawing on my part.

Having said that though, I also considered the use of 2 teensy's in a similar way you describe but felt it might be overkill and could do the same thing with just one. So far the only drawback I see is that I may need to re-flash the Teensy whenever I compose a new set of macros to control different apps on the PC. Having a multi-function program on the Teensy to handle different target apps and selectively choosing which one runs is the challenge as is keeping the whole thing small enough to fit in the Teensy's memory.

Thanks also for the link.
 
Regarding "what software you need", I meant there are libraries and applications that help.

Ahh OK, gotcha now. Yes, I've seen several potential candidates already on the Teensy site with some references to Github. More to see there I'm sure.
 
... lots of good info ...


Hi codingisfun, well that was an education in itself. I didn't know about the K90 (or that there might be such sophisticated keyboards already on the market) - and I'm sure that the information I saw about the need for software running and being detected on the PC must have applied to lower-end wannabe keyboards. Having looked at that particular keyboard it's tempting to just race out and buy something like that but I'd lose the "fun" of building a solution myself and the precious knowledge gained by going the DIY route.

I'm a little confused about the need for both the Arduino and Teensy environment - Am I to assume that the Teensy environment is somehow lacking something that the Arduino provides? Or is there another reason for using both environments? I'm unfamiliar with either, so excuse my ignorance on this, perhaps you can further enlighten me?

Also, given that you seem to have a pretty good grasp on the Teesny thing - which "model" do you think I should choose for my project, furthermore, I'd be interested to know your reasoning behind the choice as well?
 
The Teensy environment is an overlay on top of the Arduino environment. I can still program my Uno with the Teensy environment by changing the machine from Teensy whatever to Uno. Then it just uses the standard Arduino libraries, etc.

The Teensy software adds the libraries, fixes to the Arduino libraries for the Teensy, adds the ARM GCC toolchain and the special sauce needed to make the Teensies work.
 
Last edited:
Hi codingisfun, well that was an education in itself. I didn't know about the K90 (or that there might be such sophisticated keyboards already on the market) - and I'm sure that the information I saw about the need for software running and being detected on the PC must have applied to lower-end wannabe keyboards. Having looked at that particular keyboard it's tempting to just race out and buy something like that but I'd lose the "fun" of building a solution myself and the precious knowledge gained by going the DIY route.

I'm a little confused about the need for both the Arduino and Teensy environment - Am I to assume that the Teensy environment is somehow lacking something that the Arduino provides? Or is there another reason for using both environments? I'm unfamiliar with either, so excuse my ignorance on this, perhaps you can further enlighten me?

Also, given that you seem to have a pretty good grasp on the Teesny thing - which "model" do you think I should choose for my project, furthermore, I'd be interested to know your reasoning behind the choice as well?

Hi Xennon,

As MichaelMeissner already pointed out: Arduino is an "overlay" to Teensy. Maybe let me elaborate a little.

Arduino is on the one hand, a hardware standard, and on the other, a software development environment fitted to that standard. The original hardware standard (Arduino UNO, see https://www.arduino.cc/en/Main/arduinoBoardUno) is for a board about 7cm x 5cm, with a simple AVR microcontroller, and standardized connectors that let you attach other hardware ("Shields" in Arduino terminology) to it. Those "Shields" have the male part of the connectors that you see on the Arduino UNO images running as female connectors along two sides. The idea is that you can plug a shield (with for example a GPS receiver, or a set of motor interfaces) into the Arduino, load the appropriate software accessing it onto the microcontroller, and get some cool device through that. There are many shields - to get an idea just search for Arduino shields.

The software development environment is basically a software tool that lets you write C++/C programs for the Arduino with some special adaptations. It includes the code to correctly initialize the microcontroller, and then has two functions you need to write: "setup" including the code that is executed once when the microcontroller is powered on (i.e. this is stuff to initialize your device), and "loop" including the code that is continously executed while the controller is running. Arduino also features special commands to turn on and off individual pins ("digitalWrite", or "analogWrite" for analog signals), to read them in ("digitalRead" or "analogRead"), that make it easy to get going. The Arduino environment presents you with an editor for your program (called a "sketch"), from which you can directly translate and download your program onto the Arduino board.

Now Teensy: Arduino is relatively large (as a board) and features a quite small controller. So people have started to develop variants of Arduino, which have a smaller footprint (like Teensy), a more powerful microcontroller (like Teensy 3), or additional hardware on board (such as a WLAN or Bluetooth radio transmitter), or any combination of such aspects. I only know Teensy 3.1 from personal experience, so I cannot comment on the others. The software environment for Teensy is the same, and almost all Arduino software runs directly on the Teensy as well. When you look at the boards, you can see that you cannot attach an Arduino shield to a Teensy (the connectors just do not fit, and they do have a different pin layout). You usually can attach an Arduino shield with cables to a Teensy, but it is messy. There are also a couple of Teensy "shields" that directly fit onto a Teensy, but the variety is much smaller. So, if you want a lot of different "hardware shields" to play with and are not so concerned about size or performance, go for an original Arduino. If you want something small for your project, go for the Teensy. I have gone for Teensy 3.1 because I needed the compute power (at least 10 times higher than on the original Arduino), the memory (64 kBytes of RAM vs. 2 kBytes on the original Arduino), and some of the additional interfaces that Teensy has.

I believe that probably any Teensy would work for your purpose. Since Teensy 3.1 is the most powerful variant and you are not yet sure on all you want to do with it, and as price differences are small, I would go for that. BTW, the LC looks cool too, but it is mainly a less costly version of Teensy 3.1 (with less memory and somewhere around a third of the compute power, especially targeted for "Internet-of-things" applications). As it is new and there is less experience in the community, using it would be an additional risk for you (I would expect most current Teensy stuff to run on it without any problem, but then, there are slight differences, and for a beginner it might be very hard to figure them out).

I hear that you are still concerned about memory. You really should not. If you "hardcode" your mappings into the program (by defining them as constants), they will end up in the Flash memory of Teensy, of which you have 256 kBytes on Teensy 3.1. Trust me, I have been solving way more complicated problems on Teensy than what you are trying to do, and I cannot remember having gone over 30 kBytes of Flash memory in use. Also, the RAM is plentyful for what you want to do. The only thing that is tight is the EEPROM; this is non-volatile memory (i.e. for data you want to keep when power is off) that you can write while the program is running (you cannot write the Flash memory, except when you reprogram Teensy) - this is only 2 kBytes, and if you want to come up with some ways how you can define new key mappings without reprogramming Teensy, you would need that. But then, it might not be too small: You could still have something like 100 mappings with a length of 10-20 keystrokes stored there easily.

For having different mappings activated on your connector box, you could easily attach a couple of switches to Teensy that let you choose. Reading in their position really is simple. This is what it looks like:

const int switchPins[] = { 21, 22, 23 }; // defines that your buttons are connected to pins 21, 22, 23, in this case 3 buttons

int whichSwitchIsOn() // return the number of the switch that is turned on, -1 if none
{
int i;

for (i = 0; i < 3; i++) {
if (digitalRead(switchPins) != 0) return i;
}
return -1;
}

You can call this procedure and it returns, which switch is turned on, prioritizing the first switch over the second and over the third (i.e. if both the first and the second are turned on, it tells you the first is on; obviously, you could assign some meaning to both being turned on and return that).

I hope this helps! Good luck,
CodingIsFun
 
Last edited:
Note, if you are using EEPROM memory, you want to use the Teensy 3.1 and not the Teensy LC. The LC has a very small EEPROM memory, and it is not set up for multiple writes.

You might or might not need EEPROM memory. If you are going to do the macro coding at program time, you will not need EEPROM. If however, you want to use a special button that programs a key to do a bunch of characters, and you want the Teensy to save the information between power cycles, then you want to use non-volatile memory like EEPROM.

Beyond EEPROM, you can get a SPI flash memory controller from onehorse (you will need to solder this in, but it is designed for the Teensy): https://www.tindie.com/products/onehorse/spi-flash-memory-add-ons-for-teensy-3x/

Alternatively, you could put a micro-SD card on your Teensy.
 
Status
Not open for further replies.
Back
Top