Click here to Skip to main content
15,899,634 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!
I have a problem. I want to send from a html to a c# program the keyboard events.
When i press the SHIFT it works, but if i doesn't press SHIFT, it is dummy :(
Can somebody help me to write a right code for it?
I use json in my script, what sends to to my program the right ACSII codes, my program receives it, but it is not correct :S

C#
$("body").bind("keypress", function (event) {
        chCode = ('charCode' in event) ? event.charCode : event.keyCode;
        char = String.fromCharCode(event.which);
        console.log("char:" + char + " which:" + event.which + " keyCode:" + event.keyCode + " charCode:" + event.charCode);
        json = '{"eventID":"keypress","charCode":"' + event.which + '"}';
        ws.send(json);
    });



C#
case "keypress":
                    charCode = (string)(jsonObj["charCode"]);
                    byte keyCode = (byte)Convert.ToInt32(charCode);
                    str = Convert.ToString(Convert.ToChar(Convert.ToInt32(charCode)));
                    Console.WriteLine(str);
                    Console.WriteLine(keyCode);

                    const int KEYEVENTF_EXTENDEDKEY = 0x1;
                    const int KEYEVENTF_KEYUP = 0x2;
                    keybd_event(keyCode, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
                    keybd_event(keyCode, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
                    break;


Thank you for helping me, and sorry my bad english!
Posted
Updated 23-Mar-12 1:26am
v2
Comments
[no name] 23-Mar-12 7:27am    
Spelling mistakes corrected.
perilbrain 24-Mar-12 17:54pm    
Why dont you consult http://api.jquery.com/keypress/ once ,it seems perhaps you are missing something, as charchode is coming undefined.
Its just a suggestion I am not fully aware of jquery.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900