Following this question, I want to detect the Shift + Enter keyboard combination in an textarea
element. Consider the code block below:
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="x-ua-compatible" content="ie=9" />
<title>test</title>
<HTA:APPLICATION APPLICATIONNAME="test" />
<style type="text/css">
textarea {
resize: none;
overflow: auto;
min-height: 50px;
max-height: 200px;
width: 100%;
}
</style>
<script language="javascript">
function auto_grow(element) {
element.style.height = "5px";
element.style.height = (element.scrollHeight) + "px";
}
</script>
<SCRIPT Language="VBScript">
Sub onkeydown_script()
If window.event.Keycode = 16 Then
Msgbox "You pressed the Shift key."
End If
End Sub
</SCRIPT>
</head>
<body>
<textarea oninput="auto_grow(this)" onkeydown="onkeydown_script()" onkeyup="onkeyup_script()" wrap="off"></textarea>
</body>
</html>
where I tried to detect the Shift upon onkeydown
event and failed. I would appreciate if you could help me know what is the canonical method to detect the Shift + Enter keyboard combination.
P.S.1. Any other suggestions to improve the code above is also highly appreciated.
P.S.2. The solutions don't have to be in VBScript. In fact, JScript solutions are even better given that's the language I better understand.
P.S.3. I would appreciate that the folks reading this post consider that JScript and JavaScript are similar but not identical languages. Samwise HTAs are not conventional HTML documents per se. Neither HTML and JavaScript have been tagged here, so please consider that.
P.S.4. IMHO, the canonical solution should be either using the specific Keycode
for the keyboard combination, or there should be a global variable that indicates the state of the Shift, or if such a predefined variable doesn't exist, we may define one ourselves. I tried the shiftKey
property from this page and this page, neither seem to work.
P.S.5. Surprisingly enough, the example on this page works fine.
P.S.6. Sadly this post has been marked as a duplicated of this one, so here I will explain why this decision is unjust:
- It is not for a key combination but only one key, which is relatively easy
- The posts are VBScript exclusively while mine also includes JScript
- And finally, the post doesn't have an accepted answer
more information here.