ASP.NET - How to fire Javascript on space bar hit

Asked By Vidyasagar Patil
10-Feb-12 07:29 AM


 Hi

 How to fire a javascript event on spacebar click.....I want to write this function on Master Page So it should work throughout my project................



 There are buttons on every page...and on every  page i have called a javafunction on click of the button...so its not posssible for me to make change to all the functions... iwant a global function which will take care of this...like wether i hit the enter button or space bar the javafunction should be fired


Please paste ur code here

Thanks in advanced
  [)ia6l0 iii replied to Vidyasagar Patil
10-Feb-12 08:14 AM
Associate the key down event to the document. Put this under the script tag in your master page, or in a js that is referred in the master page. 

$(document).keydown(function(e)
{
var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
if (key == 32)
alert("space bar is pressed");
}
);

But, why on earth would you want to capture space key press across your application?
  kalpana aparnathi replied to Vidyasagar Patil
10-Feb-12 02:10 PM
hi

Use below code for solution:

function addNewKeys(event) 
{

   
if(!event) var event = window.event; // cross-browser shenanigans
   
if(event.keyCode === 32) { // this is the spacebar
        saveTdsAddNew
(event);
   
}
   
return true; // treat all other keys normally;
}

<a tabindex="7" style="cursor: pointer;" class="button" id="saveTocAddNew" onClick="saveTdsAddNew();"><span>Space bar Hit</span></a>
Create New Account
help
Net hi friends Any one send frequently asked Important questions in C# .Net, ADO .Net, Asp .Net and Sql Server. . . . . . . . tx in Advance. . . . . . Hi, Find this. . (B)What is an IL? (B A) What is scavenging? (B) What are different types of caching using cache object of ASP.NET? (B) How can you cache different version of same page using ASP.NET cache object? (A) How will implement Page Fragment Caching? (B) Can you compare ASP.NET sessions with classic ASP? (B) Which are
in Sql server as var_name int How do you separate business logic while creating an ASP.NET application? There are two level of asp.net debugging 1. Page level debugging For this we have to edit the page level debugging enable the trace to true in the line in the html format of the page. %@ Page Language = ”vb” trace = ”true”AutoEventWireup = ”false” Codebehind = ”WebForm1.aspx.vb” Inherits = ”WebApplication2.WebForm1?&gt; 2
faqs on masterpages Hi all, can anybody tell me the FAQs on Masterpages in asp.net. Thanks alot var Hi What are Master Pages in ASP.NET? or What is a Master Page? ASP.NET master pages allow you to create a consistent layout for the pages in your application A single master page defines the look and feel and standard behavior that you want for all of the
Tracing in ASP.NET? hi all, what is tracing? how to achieve tracing in asp.net? different ways of doing tracing? thanks and regards Aman Khan hi. . Tracing in ASP.NET 2.0 Tracing is a way to monitor the execution of your ASP.NET application. You can record exception details and program flow in a way that doesn't
control when it receives focus it somewhat related to controls being used i am having asp.net application in which i have used few asp:button controls some html input controls & some control is set to defaultbutton on form load .so whenever i press ENTER on the page anywhere the defualtbutton is fired its wrking fine i have few more button for different enter key no matter where the focus is will fire the default button's click event, what you can suggest is to go with pressing the space bar key instead of enter to fire the onclick event for non default buttons. anything else possible to achive the needed functinality end of post If you have many buttons in your page, what you can do to be able to control which button gets fired / executed when