BPD1 BPD1 - 1 year ago 119
Javascript Question

How to find previously pressed key in javascript

I want to find what key was pressed previously using Javascript using approach which is cross-browser compatible.

Answer Source

I guess you are looking for a solution like this.


<textarea rows="4" cols="50">
    Just start typing here ... 

    This is the previously pressed key: <span id="output"> </span>

JavaScript (jQuery):

var pressedKeys = [];
var lastKey;

$('textarea').on('keydown', function(e) {
    lastKey = pressedKeys[pressedKeys.length - 2]


Watch it on CodePen here: http://codepen.io/MarkBuskbjerg/pen/Yppbrg

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download