What is the best cross-browser and cross-platform way to detect hardware keyboard presence with javascript?
-
3Can you have the user press a key that doesn't exist on a touchscreen keyboard? Like anything that uses `Ctrl` or `Alt`? – millimoose Sep 18 '13 at 19:03
-
3@millimoose [nuh uh](https://play.google.com/store/apps/details?id=org.pocketworkstation.pckeyboard&hl=en) – Brian Sep 18 '13 at 19:04
-
I really need "Enter", then keyCode 13. – user2792858 Sep 18 '13 at 19:07
-
Are you looking to tell if the user has a hardware keyboard vs a virtual/software keyboard, or just any keyboard? – j08691 Sep 18 '13 at 19:07
-
@Brian Eh, if someone goes to the bother of installing that app they deserve the consequences of whatever the OP is doing not working. – millimoose Sep 18 '13 at 19:07
-
2Aaanyway. XY Problem. What are you ultimately trying to accomplish? Why do you need to detect this? Give us a little context. – millimoose Sep 18 '13 at 19:08
-
What's your use case? Maybe there's another way to detect the features you need. – pawel Sep 18 '13 at 19:11
-
1You know the facebook chat? You send messages simply by pressing "Enter", I have to show users that do not have a keyboard button to replace the "Enter" key. – user2792858 Sep 18 '13 at 19:16
-
To put it differently: seeing as there's no Javascript API for doing this, the a priori probability is pretty high there is no 100% reliable way of doing this. Describing your use case would help us find out what hack / workaround would be suitable. – millimoose Sep 18 '13 at 19:16
-
@user2792858 Just include an icon regardless of whether or not the user has a keyboard? Or whenever that's in doubt, like with any mobile OS. Also most virtual keyboards do have a return key that might send a usable keycode. – millimoose Sep 18 '13 at 19:17
-
Yes, it is true, but I ruin the design... – user2792858 Sep 18 '13 at 19:18
-
You ruin the design? But you have to display the button for certain devices anyway, so maybe rethink the design? Anyway, another option to think about: display the button but as soon as the user sends the message with a return key press, hide it. – Ingo Bürk Sep 18 '13 at 19:28
-
3a good design is consistent, show the button to everyone. why assume people know about [enter] anyway? more ways to do stuff usually means more productivity. you can dbl-click a windows title bar to nmaximize it, or you can use a key combo, or the maximize icon, or the top-left window menu, or the taskbar context menu, etc. nobody complains that there are too many ways to maximize... – dandavis Sep 18 '13 at 20:25
-
Just a [very late] thought to add... You *might* be able to tell if the on-screen keyboard has popped by examining some properties of the viewport and/or page elements. I doubt browsers fire the `resize` event when the keyboard pops, but you might be able to examine viewport height (or visibility of an element) when the page loads, and compare that same property once an input element has received focus (causing the on-screen keyboard to automatically appear in most OSes). It probably *won't* be very cross browser compatible if it even works in any of them, but it could be a start. – Dusty Sep 10 '15 at 18:34
-
I think there _are_ valid use-cases for this besides hiding/showing a button based on the presence of a keyboard. I, for one, would like to auto-focus an input field when a dialog panel opens, but this impairs usability with a virtual keyboard that pops up out of nowhere. – John Weisz Oct 15 '16 at 19:54
6 Answers
This may be an old question, but a few months ago, I was looking for a solution to this myself. I was building a messaging system which should send the message when someone hits Return
on their physical keyboard, but inserts a newline when someone hits Return
on a virtual keyboard. The way I solved it was by counting the time between keydown
and keyup
events and getting the average when Return
was hit.
I finally got around to documenting it on my blog here.
-
2Very clever, I ended up using this solution in my own application. Thank you! – Nigel Nelson Dec 21 '18 at 02:14
-
-
3This is a reliable solution with one major dependency: the user needs to type something first. The holy grail is to reliably detect whether the user has only a software keyboard when the user lands on the page, before they do any typing. – thdoan Jun 25 '22 at 01:10
-
This won’t work if the user switches from a virtual keyboard to a physical (or vice versa) while the app is open – I think on tablets this is a realistic issue. Would be pretty hard for the user to figure out what’s going on – especially if the page was open for a long time (say, an installed PWA). – felixbade Mar 13 '23 at 12:06
Could you try the theoretical opposite? Instead of trying to detect keyboard hardware, why not try to detect a touch screen? With the ontouchstart
event;
if ('ontouchstart' in document.documentElement) {
// show icon
}

- 963
- 1
- 8
- 18
-
22
-
5Well, what would you do if an iPad has a keyboard attached to it? It sounds like a lot of code to show a simple icon. – bmorenate Sep 18 '13 at 19:47
-
1Many blackberry devices have both touch screen and physical keyboard – Anonymous Dec 07 '17 at 12:14
-
I assume many touch-screen PCs may have an issue with this, but at least it is a way to detect some of the newer iPads (which previous Regex check solutions do not seem to work with, specifically in the mobile Safari browser), so +1 for the creative solution – Broper Aug 31 '22 at 18:34
Keyboard in JS is accessible via browser APIs which delegate to OS APIs and it's not possible to tell if there's a physical keyboard. I can cut the cord off of my physical keyboard right now, turn on virtual keyboard, click on the on-screen buttons with my mouse and the browser will still trigger every keyboard event the scripts are listening to. Form browsers's/JS's perspective the virtual keyboard is indistinguishable from a physical one.
And what does "presence" even mean? If I have a phone with a touch screen and slide-out keyboard do you expect the browser to trigger some kind of "keboardIn"/"keyboardOut" event? Same with cable plug-in/out? :)
If your app absolutely requires a physical keyboard just inform/ask the user.
Edit - after clarification by OP:
You know the facebook chat? You send messages simply by pressing "Enter", I have to show users that do not have a keyboard button to replace the "Enter" key.
So just make a form with text input and listen to the input/form events. Most (every?) soft keyboards have some kind of "Done", "Ready" or similar button. You don't need to know if the "keyCode" is equal to "13", but detect that the user has an intent to submit what he has typed. Or, as the last resort, detect f the device i touch-enabled and display the button then. ( if('ontouchstart' in document.documentElement)/* show touch sbmit button */
)

- 35,827
- 7
- 56
- 53
-
2I am making a game that by default uses keyboard to control, but I want to detect if there is no keyboard present so I can put on screen controls. – Pete May 10 '18 at 18:08
Use keyboard event to detect if the user have keyboard or not (he/she may press it). Save it in localStorage and the browser will remember it for the next time.
var app = this;
app.hasKeyboard = false;
this.keyboardPress = function() {
app.hasKeyboard = true;
$(window).unbind("keyup", app.keyboardPress);
localStorage.hasKeyboard = true;
console.log("has keyboard!")
}
$(window).on("keyup", app.keyboardPress)
if(localStorage.hasKeyboard) {
app.hasKeyboard = true;
$(window).unbind("keyup", app.keyboardPress);
console.log("has keyboard from localStorage")
}

- 132,869
- 46
- 340
- 423

- 77
- 2
-
2This won't tell you what kind of keyboard it is (hardware or virtual) – Adam Taylor Apr 09 '21 at 19:52
When a virtual keyboard pops up on the screen on a mobile device, the height of your application reduces in order to accommodate the virtual keyboard. So, what you can do is that you can add an event listener that checks whether the screen has resized as the user focuses on the input field.
You can add this functionality using the resize event listener when the input field is focused:
const inputField = document.querySelector(".my-input");
const virtualKeyboardDetected = () => alert("Virtual keyboard detected!");
inputField.addEventListener("focusin", () => {
window.addEventListener("resize", virtualKeyboardDetected )
})
inputField.addEventListener("focusout", () => {
window.removeEventListener("resize", virtualKeyboardDetected )
})

- 612
- 1
- 6
- 13
if (confirm('Do you have hardware keyboard?')) {
} else {
}
Edit according to description in comments under question:
What about support 'Enter' everytime and add 'Send' icon only for touch screens (What's the best way to detect a 'touch screen' device using JavaScript?) and as a 'hover' pseudoclass for mouse?

- 1
- 1

- 1,244
- 1
- 10
- 18
-
1But can you imagine if every page facebook asked "Do you have hardware keyboard?" :D – user2792858 Sep 18 '13 at 19:09
-
4
-
1It is uncomfortable, even saving it to a database, if the user logs first from pc and then to phone? It is not a good solution... – user2792858 Sep 18 '13 at 19:26
-
3Yes maybe uncomfortable, but I would recommend you to study something about HTTP cookies, then you will know that cookies are unique per browser. – René Kolařík Sep 18 '13 at 19:34
-
29
-
3
-
-
1Reading this in 2021 when websites already ask you if they can use cookies, ask for location permission, turn on notifications, turn off your ad blocker, subscribe to their mailing list, etc etc. No need to add a keyboard prompt to the ever-growing list. – nixin72 May 31 '21 at 14:28