0

Ok, before you mark this as a duplicate, read the title.

I know that you can do this:

var triggerevent = jQuery.Event("keypress");
triggerevent.which = 52; // 4
$("#inputfield").trigger(triggerevent);

But what if I wanted to trigger the pressing of the '$' sign for example? You can't do that without pressing shift beforehand.

How is that possible?

Lucas
  • 16,930
  • 31
  • 110
  • 182
  • Get the id of the key and use `e.shiftKey`: http://stackoverflow.com/questions/11468644/click-to-replace-value-shift-click-to-append-value-using-jquery/11468883#11468883 – Simon Sep 10 '12 at 07:58
  • @Simon could you put that in an answer? – Lucas Sep 10 '12 at 07:58
  • It says: "Trivial answer converted to comment" ... – Simon Sep 10 '12 at 08:00
  • Anyway I may have misread your question, you don't want to register the physical keypress along with shift, but trigger a shift+keypress in jQuery? – Simon Sep 10 '12 at 08:02
  • @Simon Yes like writing `$`: shift + 4, I don't know exactly what this falls under – Lucas Sep 10 '12 at 08:05
  • Before I try to give a definitive answer: What is your aim here? Why do you have to trigger the key pressing instead of calling the trigger functions directly or just inserting the value? – Simon Sep 10 '12 at 08:12
  • @Simon I have to simulate the keypress, so the program can pick it up. – Lucas Sep 10 '12 at 08:13

2 Answers2

1
var triggerevent = jQuery.Event("keypress");
triggerevent.which = 36; // $

$("#textarea").on('keypress', function(e) {
    if(!e.hasOwnProperty('originalEvent')) {
        var c = String.fromCharCode(e.which); // $
        this.value = c;
    }
}).trigger(triggerevent);

jsfiddle

Update: insert character programmatically, check if physical keypress occurred

Update 2: removed the whole shiftKey thing, because not of importance anymore

Simon
  • 7,182
  • 2
  • 26
  • 42
  • sure: http://jsfiddle.net/26ru6/, inspect the event object which I log to the console, you'll see that "shiftKey" is a simple property which the plugin reads out (like in the link from my first comment), so no magic there. – Simon Sep 10 '12 at 09:21
  • Then why doesn't this work: http://jsfiddle.net/rGdUf/1/ ? It should work, shouldn't it? – Lucas Sep 10 '12 at 09:24
  • I don' understand, it does work, the event gets triggered, what do you expect it to do besides that? – Simon Sep 10 '12 at 09:25
  • Shouldn't it also insert it into the textarea as well? I don't know. – Lucas Sep 10 '12 at 09:27
  • Since it is your "custom" event it does not execute the actual inserting of a character, if you open the console in the fiddle and type a character into the textarea you'll see a difference in the event objects, the event fired on keypress has a property "originalEvent" which represents the before mentioned physical pressing of a key... you cannot simulate that (see [trigger keypress events](http://stackoverflow.com/questions/832059/definitive-way-to-trigger-keypress-events-with-jquery)). I thought you just wanted to trigger the event so a plugin or external code can grab it? – Simon Sep 10 '12 at 09:39
  • Oh well, thanks anyway. Still found a way around my original problem. Marking as accepted right now. – Lucas Sep 10 '12 at 09:41
  • Yeah forgot that, you know, when pressing shift+4 another keycode than 52 is registered, maybe we should just go for that... – Simon Sep 10 '12 at 09:51
  • Well shift+4 gets me 231 on my keyboard, thought it would get you an $, just log the event in my fiddle, by typing your $ you'll get the keycode ;). – Simon Sep 10 '12 at 10:02
0

since the event object in jQuery is derived from the native DOM event, you can use those attributes to check which combination has been pressed.

DOM W3 Reference (Check the attributes section)

KyorCode
  • 1,477
  • 1
  • 22
  • 32