用js改变键盘事件做一个简单的改建效果上下左右控制方格效果
用js代码来改变按钮事件,先获取键盘事件,然后转换为字节码,字节码值对应相应的键盘事件,来改变对应默认的值
1、以下是演示效果
2、以下是代码演示,思路,吧获取到的键盘事件转换为event.keyCode值,再用event.keyCode值给相对应更改的键盘值
3、以下是html代码块
<img src="img/eclipse_update_120.jpg" id="imgShow" style=" width: 70px; height: 70px; position:absolute; top: 120px; left: 0px;"/>
上:<input type="text" id="top" onkeydown="top1()"/><br />
下:<input type="text" id="bottom" onkeydown="bottom1()"/><br />
左:<input type="text" id="left" onkeydown="left1()"/><br />
右:<input type="text" id="right" onkeydown="right1()"/><input type="button" value="更改" onclick="dianji()"/>
<span>默认:↑ ↓ ← →</span>
4、下面是js代码块
<script type="text/javascript">
var img = document.getElementById("imgShow");
img.left = 0;
img.top = 120;
var topp;
var gTop = 38;
var bottom;
var gBtooom = 40;
var left;
var gLeft = 37;
var right;
var gRight = 39;
function top1(){
topp = event.keyCode;
}
function bottom1(){
bottom = event.keyCode;
}
function left1(){
left = event.keyCode;
}
function right1(){
right = event.keyCode;
}
function dianji(){
//上
gTop = topp;
//下
gBtooom = bottom;
//左
gLeft = left;
//右
gRight = right;
}
function onclik(){
//左37
//上38
//右39
//下40
var a = event.keyCode;
if(a == gLeft){
img.left-=70;
}
if(a == gTop){
img.top-=70;
}
if(a == gRight){
img.left+=70;
}
if(a == gBtooom){
img.top+=70;
}
img.style.left = img.left+'px';
img.style.top=img.top+'px';
}
</script>