Share via

Resolve the event issue

Jonathan 65 Reputation points
2026-02-04T02:32:05.3966667+00:00

Hi,

As event was deprecated, how to correct it below?

User's image

Developer technologies | ASP.NET | Other
0 comments No comments

Answer accepted by question author
  1. Jack Dang (WICLOUD CORPORATION) 16,040 Reputation points Microsoft External Staff Moderator
    2026-02-04T10:03:46.7333333+00:00

    Hi @Jonathan ,

    Thanks for clarifying.

    You can simplify your function like this:

    function testKeyCode(e) {
        e = e || window.event;       // fallback for old browsers
        var keycode = e.keyCode;     // numeric key value
    
        if (keycode === 37 && e.altKey) {
            CallButtonClick2();
            window.history.back();
        }
    }
    
    document.onkeydown = testKeyCode;
    

1 additional answer

Sort by: Most helpful
  1. Jack Dang (WICLOUD CORPORATION) 16,040 Reputation points Microsoft External Staff Moderator
    2026-02-04T07:50:22.78+00:00

    Hi @Jonathan ,

    Thanks for reaching out.

    Instead of your current line:

    if (window.event) keycode = window.event.keyCode;
    

    You can use a modern approach like this:

    document.addEventListener('keydown', function(event) {
        let keyPressed = event.key; // gives the key like "a", "Enter", etc.
        console.log("Key pressed:", keyPressed);
    });
    

    Here, event is automatically provided, and event.key is more readable than keyCode. If you’re using this on an ASP.NET page, this works seamlessly with your existing <asp:TextBox> or other elements.

    Hope this helps! If my answer was helpful - kindly follow the instructions here so others with the same problem can benefit as well.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.