Closed Thread
Results 1 to 4 of 4

Thread: onkeypress

  1. #1
    Divya is offline Learning Programmer
    Join Date
    Apr 2008
    Location
    India
    Posts
    32
    Rep Power
    0

    onkeypress

    I just want to call an ajax function on pressing the enter key and alt key.since i have a submit button,the form gets submitted if i press enter.Instead,an ajax function should be called when i press enter or alt.Also,i just want to do it with the help of ASCII values of those two buttons on keypress event.Help me!!

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: onkeypress

    You will need to execute a function onkeypress:

    Code:
    function keypressed(evt){
      var key = (evt) ? evt.which : event.keyCode;
      alert(String.fromCharCode(key));
    
      // Later .....
      if (String.fromCharCode(key)=="<char here>"){
          // Do something
      }
    }
    Right now it will just alert you to the key pressed. Add this function to the onkeypress event as <input type="text" onkeypress="keypressed(event)">

  4. #3
    jhonichen's Avatar
    jhonichen is offline Newbie
    Join Date
    May 2009
    Location
    Jakarta, ID
    Posts
    20
    Rep Power
    0

    Re: onkeypress

    try below
    Code:
    document.onkeydown=function(evt) {
    	var isAltKey = (evt) ? evt.altKey : event.altKey;
    	var key = (evt) ? evt.which : event.keyCode;
    	if(isAltKey  && key == 13) {
    		//do ajax call here
    	}
    };

  5. #4
    Parabola's Avatar
    Parabola is offline Programming Professional
    Join Date
    Jul 2009
    Location
    Texas
    Posts
    336
    Blog Entries
    4
    Rep Power
    13

    Re: onkeypress

    EDIT: realized google had taken me into AJAX, not c#

    Jordan, if you want to remove this, please do- wrong language
    my apologies everyone... thought things looked odd lol

    original post:
    I know this is an older topic, but I found something else while working on a project. I too wanted the enter key to do something, here's what I came up with:

    Code:
            private void getData()
            {
                try
                {
                    this.inventory_pricingTableAdapter.Fill(this.marksDataSet.inventory_pricing, partNumToolStripTextBox.Text);
                }
                catch (System.Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }
            }
    
            private void partNumToolStripTextBox_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (e.KeyChar == 13)
                {
                    getData();
                }
            }
    Works great for me
    Last edited by Parabola; 08-11-2009 at 07:35 AM. Reason: wrong language
    Programmer (n): An organism that can turn caffeine into code.
    Programming would be so much easier without all the users.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Function Onkeypress for cursor key
    By newphpcoder in forum JavaScript and CSS
    Replies: 1
    Last Post: 11-10-2010, 02:08 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts