📄 在j2me中基于midp1_0实现组合按键-开发者网络-j2me-天极yesky.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<!-- saved from url=(0038)http://dev.yesky.com/421/2313421.shtml -->
<HTML lang=zh-CN xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>在J2ME中基于MIDP1.0实现组合按键-开发者网络-J2ME-天极Yesky</TITLE>
<META content=在J2ME中基于MIDP1.0实现组合按键, name=description>
<META content=在J2ME中基于MIDP1.0实现组合按键, name=keywords>
<META content="天极Yesky | 全球中文IT第一门户" name=author>
<META content="天极Yesky | 全球中文IT第一门户" name=Copyright>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META http-equiv=Content-Language content=zh-CN><LINK media=all
href="在J2ME中基于MIDP1_0实现组合按键-开发者网络-J2ME-天极Yesky.files/y_mobile_content.css"
type=text/css rel=stylesheet>
<SCRIPT
src="在J2ME中基于MIDP1_0实现组合按键-开发者网络-J2ME-天极Yesky.files/showlay.js"></SCRIPT>
<META content="MSHTML 6.00.2900.3132" name=GENERATOR></HEAD>
<BODY><!--页面头部--><!--头部-->
<DIV id=topmenu><A href="http://www.yesky.com/">Yesky首页</A>| <A
href="http://product.yesky.com/">产品报价</A>| <A
href="http://cseek.yesky.com/">行情</A>| <A href="http://mobile.yesky.com/">手机</A>
| <A href="http://digital.yesky.com/">数码</A> | <A
href="http://notebook.yesky.com/">笔记本</A> | <A
href="http://pc.yesky.com/">台式机</A> | <A href="http://diy.yesky.com/">DIY硬件</A>
| <A href="http://oa.yesky.com/">外设</A> | <A href="http://net.yesky.com/">网络</A>
| <A href="http://dh.yesky.com/">数字家庭</A> | <A
href="http://lab.yesky.com/">评测</A> | <A href="http://soft.yesky.com/">软件</A> |
<A href="http://e.yesky.com/">e时代</A> | <A href="http://game.yesky.com/">游戏</A>
| <A href="http://pic.yesky.com/">图片</A> | <A
href="http://desktop.yesky.com/">壁纸</A> | <A href="http://q.yesky.com/">群乐</A> |
<A href="http://my.yesky.com/">社区</A> | <A href="http://blog.yesky.com/">博客</A>
| <A href="http://www.mydown.com/">下载</A></DIV>
<DIV id=topbox>
<DIV class=tleft><A href="http://www.yesky.com/"><IMG alt=天极Yesky_全球中文IT第一门户
src="在J2ME中基于MIDP1_0实现组合按键-开发者网络-J2ME-天极Yesky.files/logo.gif"
border=0></A></DIV>
<DIV class=tad><SPAN id=ad1></SPAN></DIV>
<DIV id=search>
<DIV>
<FORM name=searchform action=http://search.chinabyte.com/search method=get
target=_blank><INPUT id=searchzz onmouseover=this.focus() onfocus=this.select()
name=q><INPUT type=hidden value=GB2312 name=encoding><INPUT type=hidden
value=connect name=dir><INPUT type=hidden value=20002 name=cid><INPUT id=search_btn type=submit value=天极搜索> </FORM></DIV>
<DIV class=tdq>
<DIV class=rightarea><A href="http://bj.yesky.com/">北京</A> <A
href="http://hd.yesky.com/">上海</A> <A href="http://gd.yesky.com/">广东</A> <A
href="http://xa.yesky.com/">西安</A><BR><A href="http://cq.yesky.com/">重庆</A> <A
href="http://cd.yesky.com/">成都</A> <A href="http://sy.yesky.com/">沈阳</A> <A
href="http://js.yesky.com/">江苏</A> </DIV>
<DIV class=rightarea_right><A
href="http://my.yesky.com/">用户<BR>登录</A></DIV></DIV></DIV>
<DIV class=tright>
<DIV class=box1>
<SCRIPT src="在J2ME中基于MIDP1_0实现组合按键-开发者网络-J2ME-天极Yesky.files/content_text_ad.js"
type=text/javascript></SCRIPT>
</DIV></DIV></DIV>
<SCRIPT src="在J2ME中基于MIDP1_0实现组合按键-开发者网络-J2ME-天极Yesky.files/flash.js"
type=text/javascript></SCRIPT>
<DIV id=childmenu><A href="http://soft.yesky.com/">软件频道></A><A
href="http://dev.yesky.com/">程序开发></A><A
href="http://dev.yesky.com/devjava/">Java</A><A
href="http://dev.yesky.com/ysdevvb">VB</A><A
href="http://dev.yesky.com/ysdevvc">VC</A><A
href="http://dev.yesky.com/ysdevdelphi">Delphi</A><A
href="http://dev.yesky.com/ysdevc">C/C++</A><A
href="http://dev.yesky.com/web/">Web开发</A><A
href="http://dev.yesky.com/msdn/">微软专栏</A><A
href="http://dev.yesky.com/devydkf/">移动</A><A
href="http://dev.yesky.com/devsjk/">数据库</A><A
href="http://dev.yesky.com/ysdevcxrs">程序人生</A><A
href="http://dev.yesky.com/ysdevrjgc">软件工程</A>|<A
href="http://soft.yesky.com/lesson/">产品中心</A><A
href="http://www.mydown.com/soft/develop/news.shtml">下载</A><A
href="http://q.yesky.com/group/179"><FONT
color=#ff0000>群乐:大家耍QQ</FONT></A></DIV><!--导航条-->
<DIV id=location>
<DIV class=loleft>您现在的位置: <A href="http://www.yesky.com/">天极Yesky</A> > <A
href="http://soft.yesky.com/">软件频道</A> > <A
href="http://dev.yesky.com/">开发者网络</A> > 在J2ME中基于MIDP1.0实现组合按键</DIV>
<DIV class=adright><SPAN id=ad2></SPAN></DIV></DIV><!--内容块开始-->
<DIV id=conbox><!--左边开始-->
<DIV id=conleft>
<DIV id=contopla1>
<DIV id=__001>
<DIV class=qkong></DIV>
<DIV class=qw1>全文</DIV>
<DIV class=zkong>
<DIV class=kl></DIV>
<DIV class=bj><A href="http://dev.yesky.com/421/2313421.shtml#pls"
target=_self>评论</A></DIV>
<DIV class=kr></DIV>
<SCRIPT type=text/javascript>
var titiepic ="#";
if (titiepic!="#")
{
document.write("<div class=\"kl\"></div><div class=\"bj\"><a href=\"#\" target=\"_blank\">图片</a></div><div class=\"kr\"></div>");
}
</SCRIPT>
</DIV></DIV></DIV>
<DIV class=topadbg><SPAN id=ad10></SPAN></DIV><A name=top></A>
<DIV id=contitle>
<H1>在J2ME中基于MIDP1.0实现组合按键</H1></DIV>
<DIV id=conauthor><SPAN>2006-02-23 14:47 </SPAN><SPAN>作者: deadspace
</SPAN><SPAN>出处: java研究组织 </SPAN><SPAN>责任编辑:<A title=向本编辑提问
href="http://comments.yesky.com/t/·½ÖÛ/6,324/2313421.shtml" target=_blank>方舟</A>
</SPAN></DIV>
<DIV class=topadbg><SPAN id=ad9></SPAN></DIV>
<DIV id=conneirong><SPAN id=ad3></SPAN>
<DIV class=guanggao><SPAN
id=contentAdv></SPAN></DIV> 我们知道有些手机是不支持重复按键功能,为了模拟重复按键的功能,我们不得不使用一个线程来进行处理。对于重复按键网上已经有一些文章进行了探讨,其实利用线程我们还可以实现组合按键的功能。当然,在手机这种设备上,利用组合按键进行游戏,有一定的限制,因为手机各个按键排列比较紧凑,同时按下两个甚至三个按键在操作上会有局限性。但这也是仁者见仁、智者见智的问题,如果你想提高游戏的可玩性和娱乐性,组合按键仍是必不可少的条件。<BR><BR> 我定义了一个进行按键监听的类,这个类实现了Runnable接口。在这个类里关键的一个数据结构就是一个字节型的数组,它是用来存储当前所有被按下的键。我这里把数组的长度设定为2,主要是从应用的角度来考虑的,因为毕竟在当前手机设备上同时按三个键进行游戏的不多。下面就是实现组合按键的核心代码。<BR><BR> 按键事件类:<BR><BR>
<TABLE borderColor=#cccccc width="90%" align=center bgColor=#e3e3e3 border=1>
<TBODY>
<TR>
<TD>import javax.microedition.lcdui.*;<BR><BR>public class NewEvent
implements Runnable{<BR> KeyEventCanvas c=null;<BR><BR> /*<BR> *
用来判断是否有键被按下<BR> */<BR> protected boolean switchPoint;<BR><BR> /*<BR> *
这个数组用来存储所有同时被按键的键码<BR> */<BR> protected byte[] keyCode;<BR><BR> /*<BR> *
访问数组的索引<BR> */<BR> byte index;<BR><BR> public NewEvent(KeyEventCanvas
c){<BR> this.c=c;<BR> keyCode=new byte[2];<BR> }<BR><BR> /*<BR> *
这个方法把被按键的键码存储到数组中<BR> */<BR> public synchronized void startAction(int
keyCode){<BR> this.keyCode[index]=(byte)keyCode;<BR> index=(byte)((index+1)%this.keyCode.length);<BR> }<BR><BR> /*<BR> *
当某个键被释放时,这个方法把该键存储在数组中的键码置为0<BR> */<BR> public synchronized void
stopAction(int keyCode){<BR> for(int
i=0;i<=this.keyCode.length-1;i++){<BR> if(this.keyCode[i]==(byte)keyCode){<BR> this.keyCode[i]=0;<BR> break;<BR> }<BR> }<BR> }<BR><BR> /*<BR> *
这个方法用来判断是否有被按下的键,返回true表示有键按下,返回false表示没有键被按下<BR> */<BR> public
synchronized boolean
getSwitchPoint(){<BR> this.switchPoint=false;<BR> for(int
i=0;i<=this.keyCode.length-1;i++){<BR> if(this.keyCode[i]!=0){<BR> this.switchPoint=true;<BR> break;<BR> }<BR> }<BR> return
this.switchPoint;<BR> }<BR><BR> /*<BR> * 返回字节数组<BR> */<BR> public
synchronized byte[] getKeyCode(){<BR> return
this.keyCode;<BR> }<BR><BR> public void run(){<BR> byte[]
tmp=this.getKeyCode();<BR> while(true){<BR> while(this.getSwitchPoint()){<BR> /*<BR> *
通过这个循环遍历数组,遇到被按下的键就进行处理<BR> */<BR> for(int
i=0;i<=tmp.length-1;i++){<BR> if(tmp[i]!=0){<BR> switch(c.getGameAction(tmp[i])){<BR> case
Canvas.UP:<BR> c.y--;//y是在Canvas中定义的一个属性,表示y轴坐标<BR> break;<BR> case
Canvas.DOWN:<BR> c.y++;<BR> break;<BR> case
Canvas.LEFT:<BR> c.x--;//x是在Canvas中定义的一个属性,表示x轴坐标<BR> break;<BR> case
Canvas.RIGHT:<BR> c.x++;<BR> break;<BR> }<BR> }<BR> }<BR> c.repaint();<BR> try{<BR> Thread.sleep(10);<BR> }catch(InterruptedException
e){}<BR> }<BR> }<BR>}<BR>}<BR><BR> Canvas类:<BR><BR>import
javax.microedition.lcdui.*;<BR><BR>public class KeyEventCanvas extends
Canvas{<BR> int x,y;<BR> NewEvent mke=null;<BR><BR> public
KeyEventCanvas(){<BR> mke=new NewEvent(this);<BR> new
Thread(mke).start();<BR> }<BR><BR> public void keyPressed(int
keyCode){<BR> mke.startAction(keyCode);<BR> }<BR><BR> public void
keyReleased(int
keyCode){<BR> mke.stopAction(keyCode);<BR> }<BR><BR> public void
paint(Graphics
g){<BR> g.setColor(0xffffff);<BR> g.fillRect(0,0,this.getWidth(),this.getHeight());<BR> g.setColor(0xff0000);<BR> g.fillRect(x,y,8,8);<BR> }<BR>}</TD></TR></TBODY></TABLE></DIV><!--广告代码开始-->
<SCRIPT>
var ad_cid;
if (window.location.search.substring(1) != "")
{
ad_cid = window.location.search.substring(1);
} else {
ad_cid = 412;
}
</SCRIPT>
<!--小通栏:c-scolumn顶部500*90--><SPAN id=span_ad1>
<SCRIPT language=JavaScript>
document.write("<script src=\"http://vd.yesky.com/html/"+ad_cid+"/"+ad_cid+"_c-scolumn.js\"><\/script>");</SCRIPT>
</SPAN>
<SCRIPT language=JavaScript>
document.getElementById("ad1").innerHTML=document.getElementById("span_ad1").innerHTML;
document.getElementById("span_ad1").innerHTML="";
</SCRIPT>
<!--c-pip文章正文中间240*200--><SPAN id=span_ad3>
<SCRIPT language=JavaScript>
document.write("<script src=\"http://vd.yesky.com/html/"+ad_cid+"/"+ad_cid+"_c-pip.js\"><\/script>");</SCRIPT>
</SPAN>
<SCRIPT>
if(document.getElementById("contentAdv")) {
document.getElementById("contentAdv").innerHTML=document.getElementById("span_ad3").innerHTML;
document.getElementById("span_ad3").innerHTML="";
} else {
document.getElementById("ad3").innerHTML=document.getElementById("span_ad3").innerHTML;
document.getElementById("span_ad3").innerHTML="";
}</SCRIPT>
<!--c-text-1“您现在的位置”右侧文字--><SPAN id=span_ad2>
<SCRIPT language=JavaScript>
document.write("<script src=\"http://vd.yesky.com/html/"+ad_cid+"/"+ad_cid+"_c-text-1.js\"><\/script>");</SCRIPT>
</SPAN>
<SCRIPT language=JavaScript>
document.getElementById("ad2").innerHTML=document.getElementById("span_ad2").innerHTML;
document.getElementById("span_ad2").innerHTML="";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -