How to make Textarea submit form when hitting enter in Custom HTML prompt

Hi all - My coding isn't the best and I've been trying to adapt HTML keydown Event Listener - #10 by martin so that when I press ENTER in the text area field the form submits.

Unfortunately it doesn't work, here's the HTML:

<html>

<head>
	<meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" />
	<meta content="en-us" http-equiv="Content-Language" />
	<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">

	<style type="text/css">
		body {
			background-color: black;
			font-family: "Avenir Next";
			color: #fff;
			overflow: hidden;
		}

		.buttonDiv {
			padding-top: 10px;
			display: none;
		}

		section {
			width: 100%;
			margin: 2% 0% 0 1%;
		}

		input,
		.subjectField,
		textarea {
			background: black;
			padding: 0 0 238px 0;
			color: #fff;
			border: none;
			line-height: 1em;

			width: 93%;
			font-size: 1.3em;
			vertical-align: middle;
		}

		/* hide border when input selected */
		textarea:focus, input:focus{
			outline: none;
		}
	</style>

	<script>
		function KMInit() {
			window.addEventListener("keydown", function (e) {
				if (e.keyCode == 13) {
					submitWindow(event);
				}
			});
		}



		function submit(event) {
			window.KeyboardMaestro.Submit(event);
		}
		function submitWindow(event) {
			window.KeyboardMaestro.Submit(event);
		}

		function cancelWindow(event) {
			window.KeyboardMaestro.Cancel(event);
		}

	</script>

</head>

<body data-kmwindow="SCREEN(Main,Left,20%),SCREEN(Main,Top,20%),750,300">
	<section>


		<form action="#" method="post" value="">

<textarea rows="2" cols="20" autofocus="" name="NAMEOFQuery" wrap="hard" id="subject" class="subjectField" onkeydown="if (event.keyCode == 13) { this.form.submit(); return false; }" ></textarea>
			


			<!-- ========== BUTTONS ========= -->
			<div class="buttonDiv">
				<button class="btnDefault" name="Save" type="submit" title="Press RETURN to submit"
					onclick="submitWindow('Submit');">Submit</button>
				<button class="btnCancel" name="Cancel" type="button" title="Press ESC to cancel"
					onclick="cancelWindow('Cancel');">Cancel</button> </div>
		</form>
	</section>

 
</body>

</html>

Any thoughts on how to fix? Appreciate any and all help, thanks!

The trick is:

				if (e.keyCode == 13) {
  			              window.KeyboardMaestro.Submit('OK');
  			              return false;

but I also used a local variable so the contents does not persist. And I deleted a few unused subroutines and buttons.

Without the window.KeyboardMaestro.Submit('OK'); the form variable isn't passed to Keyboard Maestro.

Textarea Blurb.kmmacros (3.5 KB)

1 Like