Click here to Skip to main content
15,909,498 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to Call a c# method while pressing the ESC key.
But my c# method not a static method.
Posted
Updated 26-Jun-14 0:33am
v2
Comments
George Jonsson 26-Jun-14 6:10am    
Do you want to call a method once when the ESC key is pressed, or do you want to call it as long as the key is pressed?
What kind of application do you have? Windows Form, Console application or what?
Prasad Avunoori 26-Jun-14 6:13am    
It's an ASP.net application. I want to call a method once only when the Esc is pressed.
George Jonsson 26-Jun-14 6:26am    
In that case I cannot really help you. ASP.net is not my area of expertise.
OriginalGriff 26-Jun-14 6:25am    
So, it's not static, and it's not a page instance method. So what is it?
Where is it defined, and how is it defined?
Prasad Avunoori 26-Jun-14 6:31am    
public void MyMethod()
{

///Code
}

It's defined in aspx.cs file.

1 solution

Into your page (or if is for more pages into your master page) you should use JavaScript and AJAX call of your method like in the next example:
<script type="text/javascript">
function onKeydown(evt) {
            if (evt != undefined &&  evt.keyCode == 27) //Esc
            {
                $.ajax({
                    type: "POST",
                    url: "/Account/OnEscHandler"  //Here you should change the URL to your method!
                });
            }
        };
        window.document.onkeydown = onKeydown;
</script>
 
Share this answer
 

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