简体   繁体   English

KeyPress事件不适用于Asp.Net中的图像控件

[英]KeyPress Event Not Working On Image Control in Asp.Net

I have tried to get key press event on image control in asp.net. 我试图在asp.net中获取图像控件上的按键事件。 But its not firing when the keys are pressed. 但是按下键时不会触发。 My code is: 我的代码是:

<head runat="server">
<title></title>
<script type="text/javascript" language="javascript">
    function keyPress(e) { alert('hi'); }
</script>
</head>

<body>
<form id="form1" runat="server">
<div>        
    <img id="img1" alt ="" width="950" height="950" onkeypress="keyPress();"  src="Images/090069_0709152912.jpg" />        
</div>    
</form>
</body>

Kindly suggest to achieve it 请建议实现它

You're trying to listen to an element that will not have focus and will not fire a keypress event. 您正在尝试收听没有焦点并且不会触发按键事件的元素。 Here's an example - You'll notice that the alert fires for the document event, but never actually alerts "test" because we cannot fire the keypress event on the image. 这是一个示例 -您会注意到该警报针对文档事件触发,但实际上并未向“测试”警报,因为我们无法在图像上触发按键事件。

Depending on what you're trying to do, you may have some options. 根据您要执行的操作,您可能有一些选择。 If all you want to do is listen globally for certain keys to change your image, then you can bind the event to the document and check for certain keys within that event handler. 如果您要做的只是全局侦听某些键以更改图像,则可以将事件绑定到文档并检查该事件处理程序中的某些键。

document.onkeypress = function(e){
    //code here
};

Here's a nice site to help with determining key codes. 这是一个帮助确定键码的好站点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM