Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am creating a calculator and need to append numbers by clicking on a div button, instead of typing on the keyboard. But it must integrate with the use of the keyboard. Now, I know how to manipulate strings to achieve this, but positioning the blinking cursor at the end of the input element line escapes me...

Is there a way to call a keyboard event directly via a function call to emulate the key strokes, so that there is no manipulation of strings?

HTML input display markup...

    <div id="displayWindow" align="right">
        <input id="textInput" type="text" align-"right" value=""></input>
    </div>

And here is where I parse the incoming mouse event data:

function keyData(ev){
 var kee = ev.target.innerHTML;
 var nums = ".1234567890";
 if (nums.indexOf(kee)>=0){
    //Append value to number input display
    //  THIS IS WHERE I NEED HELP ....

 }
 var mathOps = "-+÷×±";
 if (mathOps.indexOf(kee)>=0){
    //Prepare math operations
 }
 var chars = "MC M+ M- MR";
 if (chars.indexOf(kee)>=0){
    //MEMORY handling functions
 }
 if (kee=="="){
    //Calculate
 }
 if (kee=="C"){
    //CLEAR everything
 }
}

Any help would be appreciated...

TIA

DK

share|improve this question
1  
indexOf returns -1 in case the kee is not found, what you want to use (I think) is kee.indexOf(nums,kee) >= 0 –  Oliboy50 Jul 6 '14 at 23:55
    
@Oliboy50 ... yes you are right... I was speeding... Thank you! –  DeKoss Jul 6 '14 at 23:56
2  
Why don't you set up a Fiddle to show us what's going on ? –  adeneo Jul 6 '14 at 23:58
    
@adeneo Hmmm demonstrating data entry via keyboard is not very inspiring since it would take only an input element. And the keyboard data entry part is what I am trying to figure out... –  DeKoss Jul 7 '14 at 0:01
    
the kee is supposed to be a number (from the use of the indexOf(nums,kee), but in your code we can see this if(kee=="="), so what is it? your code is confusing on the thing you want to do. –  King King Jul 7 '14 at 0:05

1 Answer 1

This should do it!

    function simulateKeyPress(character) {
        $.event.trigger({ type : 'keypress', which : character.charCodeAt(0) });
    }

DK

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.