Best way to detect change in textbox and send typing/not typing 4 ajax

Best way to detect change in textbox and send typing/not typing 4 ajax

Hello,
I've already developed a system for doing this which isn't perfect i'm wondering if there is a better solution. Here is the code I got atm.

  1.              $(".chatmsg").live("blur", function(event) { stopTyping(); });
  2.               $(".chatmsg").live("focus", function() {
  3.                 if($(".chatmsg").val().length > 0) {
  4.                   typing();
  5.                 }
  6.               });
  7.               $(".chatmsg").live("keypress keydown", function(event) {
  8.                 var key = event.keyCode;
  9.                 
  10.                 if (key == "13") {
  11.                   event.preventDefault();
  12.                   sendMsg();
  13.                 }
  14.                 
  15.                 var msg = $(".chatmsg").val();

  16.                 if(key == "8" && msg.length == 1) {
  17.                     stopTyping();
  18.                 } else if ($.trim(msg) != "") {
  19.                     typing();
  20.                 } else {
  21.                     stopTyping();
  22.                 }
  23.               });

the textbox chatmsg class is created dynamically so I have to use live for this.

Anyway to optimize this?

I don't like the use of key == 8 and length == 1 to detect that chatmsg box is empty, but the event triggers before the val() of the chatmsg actually changes. So is there nothing better I can do?

Thanks, 
Igor