您的位置:首页 > Web前端 > JavaScript

《JavaScript学习笔记》:键盘事件

2016-06-06 20:03 393 查看

《JavaScript学习笔记》:键盘事件

上篇博文介绍了鼠标事件,本篇博文介绍下键盘事件,键盘事件有:键按下(onkeydown)和键弹起(onkeyup).

虽然只有键盘只有两种事件,但是,在用途还是相当广的,例子,在我们的qq聊天界面中,我们写好消息好,我们一般有三种方式来进行发送,如下:

1、点击”发送”按钮

2、enter

3、ctrl+enter

后面两种就是键盘事件,前面一种是鼠标点击事件。

要说明的是,当我们不知道某个键的键码时,可以使用keyCode来进行获取。

下面我们就一一进行实现。

1、点击”发送”按钮进行消息的发送

实现代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>

<script>
window.onload=function()
{
var oTxt=document.getElementById('txt1');
var oBtn=document.getElementById('btn1');
var oTxt2=document.getElementById('txt2');

oBtn.onclick=function()
{
oTxt2.value+=oTxt.value+'\n';
oTxt.value='';
};
};
</script>
</head>

<body>
<textarea id="txt2" rows="20" cols="20">
</textarea>
<br />
<input id="txt1" type="text"/>
<input id="btn1" type="button" value="发送消息" />

</body>
</html>


2、按enter键进行发送

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>

<script>
window.onload=function()
{
var oTxt=document.getElementById('txt1');
var oTxt2=document.getElementById('txt2');
oTxt.onkeydown=function(ev)
{
var oEvent=ev||event;
//alert(oEvent.keyCode);
if(oEvent.keyCode==13)//按回车提交,enter的键值为13
{
oTxt2.value+=oTxt.value+'\n';
oTxt.value='';
}
};
};
</script>
</head>

<body>
<input id="txt1" type="text"/>
<br />
<textarea id="txt2" rows="20" cols="20">
</textarea>
</body>
</html>


3、按enter和ctrl键来进行消息的发送

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script>
window.onload=function()
{
var oTxt = document.getElementById('txt1');
var oTxt2 = document.getElementById('txt2');

oTxt.onkeydown=function(ev)
{
var oEvent=ev||event;
//oEvent.ctrlKey不能写成oEvent.keyCode==17
if(oEvent.keyCode==13&&oEvent.ctrlKey)
{
oTxt2.value+=oTxt.value+'\n';
oTxt.value='';
}
};
};
</script>
</head>

<body>
<input id="txt1" type="text" />
<br />
<textarea id="txt2" rows="20" cols="20">
</textarea>
</body>
</html>


以上就是三种消息发送方式的实现,都比较简单哈。

例子二:左右移动

下面还看一个例子:用键盘的左右键来控制div的左右移动。

相信有了上面的例子,完成这个就相当easy了。

代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style>
#div1 {width:200px;height:200px;background:red;position:absolute}
</style>
<script>
window.onload=function()
{
var oDiv = document.getElementById('div1');
document.onkeydown=function(ev)
{

var oEvent=ev||event;
//alert(ev.keyCode);
if(ev.keyCode==37)//向左
{
oDiv.style.left=oDiv.offsetLeft-10+'px';
}
else if(ev.keyCode==39)//向右
{
oDiv.style.left=oDiv.offsetLeft+10+'px';
}
};
};
</script>
</head>

<body>
<div id="div1">

</div>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: