/    Sign up×
Community /Pin to ProfileBookmark

How to make onkeypress for shift + enter

Hi,

When I want Javascript make an action on a key press I use the number of the key (e.g. for enter key it is 13) Does anyone know how to make shift+enter key? (for <br> in word or frontpage)

Thanks and regards

Ceyhun

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceApr 07.2006 — <i>
</i>function chkKey(evt) {
var mykey, alt, ctrl, shift, str;
if (window.Event) {
mykey = evt.which;
alt = (evt.modifiers &amp; Event.ALT_MASK) ? true : false;
ctrl = (evt.modifiers &amp; Event.CONTROL_MASK) ? true : false;
shift = (evt.modifiers &amp; Event.SHIFT_MASK) ? true : false;
} else {
mykey = event.keyCode;
alt = event.altKey;
ctrl = event.ctrlKey;
shift = event.shiftKey;
}
if(shift
&amp;&amp; mykey == 13) {
alert("Shift-Enter was pressed");
}
return true;
}
Copy linkTweet thisAlerts:
@mrhooApr 07.2006 — Another method:

I want to set a textarea to catch shift + enter to run some code on the text instead of adding a new line:

[CODE]addEvent(txtArea_1,'keypress',con_keys);[/CODE]
Here is the key catcher- it could be a switch with multiple cases,

but I just want the one:
[CODE]function con_keys(e){
var E= allEve(e);
var boo= E.shiftKey;
var c= E.key;
if(boo && c== 13){
if(E.preventDefault ) E.preventDefault();
else if (E.returnValue)E.returnValue= false;
con_run();
}
}[/CODE]

the allEve function returns an event object with some key-event properties

for the Unknown browser:
[CODE]function allEve(e){
var ev= (window.event)? window.event: e;
if(!ev || !ev.type) return false;
var ME= ev;
ME.obj= Eve(e);
if(ME.type.indexOf('key')!= -1){
if(iz('ie') || ME.type.indexOf('keypress')!= -1){
ME.key= (ev.keyCode)? ev.keyCode: ((ev.charCode)? ev.charCode: ev.which);
}
else ME.key= ev.charCode;
if(ME.key) ME.letter= String.fromCharCode(ME.key);
}
return ME;
}[/CODE]

Here's a simple addEvent:
[CODE]
function addEvent(hoo,wot,fun){
if(hoo.addEventListener)hoo.addEventListener(wot,fun,null);
else if(hoo.attachEvent)hoo.attachEvent('on'+wot,fun);
else hoo['on'+wot]=fun;
}
[/CODE]


the iz('ie') is another story, you can leave it out for a keypress event, but you need it to catch keyup or keydown in IE
Copy linkTweet thisAlerts:
@dethlonJul 17.2006 — function chkKey(evt) {

var mykey, alt, ctrl, shift, str;

if (window.Event) {

mykey = evt.which;

alt = (evt.modifiers & Event.ALT_MASK) ? true : false;

ctrl = (evt.modifiers & Event.CONTROL_MASK) ? true : false;

shift = (evt.modifiers & Event.SHIFT_MASK) ? true : false;

} else {

mykey = event.keyCode;

alt = event.altKey;

ctrl = event.ctrlKey;

shift = event.shiftKey;

}

if(shift

&& mykey == 13) {

alert("Shift-Enter was pressed");

}

return true;

}[/QUOTE]



dear PHPnovice;

if i want the pointer in a text area to go to nextline (break one line down/ start a new text line) when the user press shift+enter....how should i modify ur code????

others who know pls tell me also thanks.......
×

Success!

Help @telmessos spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...