By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,903 Members | 1,677 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,903 IT Pros & Developers. It's quick & easy.

trap keystrokes

P: n/a
Another fellow here must modify an application. On the web page, pressing
the enter key causes the form to submit. What users want is to remove enter
key functionality from within a textarea and cause the enter key to submit
the form, AND cause the F6 key to do what the enter key used to do inside
the text area. The idea I suggested is to trap keystrokes in the text area,
If the enter key is pressed, do the submit method. If F6 is pressed, disable
the enter key test then emulate an enter key by creating an event object.
The textarea should then insert a new line as if the enter key was pressed.
would that be the right approach and has anyone done something similar.
Thanks

--
NewsGuy.Com 30Gb $9.95 Carry Forward and On Demand Bandwidth
May 18 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
The ENTER key functionality used by textarea is built into the browser
by default...I'm not sure you can trap this and use a different key
(JavaScript is much more limited than a desktop lanaguage like C++ or
Java). I would look at the "keypress" event of the textarea control,
and use window.event.KeyCode to check the keystrokes and see if you can
trap the ENTER key press and route it to another.

Larry

May 18 '06 #2

P: n/a
dr*****@yahoo.com wrote:
The ENTER key functionality used by textarea is built into the browser
by default...I'm not sure you can trap this and use a different key
(JavaScript is much more limited than a desktop lanaguage like C++ or
Java).
JavaScript, or ECMAScript implementations for that matter, was never
intended to be a "desktop language". And the features you describe are
not language features; they are features of the DOM (Document Object Model)
of the execution environment (here: browser) it runs in, which exposes
ECMAScript language binding for its interfaces to client-side scripting.
For example:
<URL:http://www.w3.org/TR/DOM-Level-2-Events/ecma-script-binding.html>
I would look at the "keypress" event of the textarea control,
True.
and use window.event.KeyCode to check the keystrokes and see if you can ^^^^^^^^^^^^^^^^^^^^ trap the ENTER key press and route it to another.


That assumes there is an object that can be referred to with window.event;
however, that is proprietary and restricted to IE only. It also assumes
that ECMAScript implementations are case-insensitive, which they are not.

A cross-browser (but due to the proprietary `event' value
not fully standards compliant) approach would be

function handleKeyPress(e)
{
if (!e) e = window.event;
if (e && e.keyCode == 13)
{
// handle Enter key
// return false; here to cancel the event
}
}

<textarea onkeypress="return handleKeyPress(event);">...</textarea>

However, users do not like it very much when you interfere with their
working environment without a very good reason for doing so:

<URL:http://codestyle.org/javascript/FAQ.shtml#suppressctrln>
PointedEars
--
Multiple exclamation marks are a sure sign of a diseased mind.
-- Terry Pratchett
May 24 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.