我正在创建一个小游戏,游戏被打印到Windows窗体上的面板上.现在我想捕获keydown事件以查看它是否已被按下的箭头键,但问题是我似乎无法捕获它.
让我解释一下,在表格上我有4个按钮和各种其他控件,如果用户例如按下其中一个按钮(触发游戏事件),那么按钮有焦点,我无法用箭头键捕捉动作.
我试过类似的东西
private void KeyDown(KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
{
game.MovePlayer(DonutWarsLibrary.GameObjects.Direction.E);
game.DrawObjects(panel1.CreateGraphics());
}
else if (e.KeyCode == Keys.Right)
{
game.MovePlayer(DonutWarsLibrary.GameObjects.Direction.W);
game.DrawObjects(panel1.CreateGraphics());
}
else if (e.KeyCode == Keys.Up)
{
game.MovePlayer(DonutWarsLibrary.GameObjects.Direction.N);
game.DrawObjects(panel1.CreateGraphics());
}
else if (e.KeyCode == Keys.Down)
{
game.MovePlayer(DonutWarsLibrary.GameObjects.Direction.S);
game.DrawObjects(panel1.CreateGraphics());
}
}
Run Code Online (Sandbox Code Playgroud)
然后当按下表单按键事件时,我使用了这个
private void MainForm_KeyDown(object sender, KeyEventArgs e)
{
KeyDown(e);
}
Run Code Online (Sandbox Code Playgroud)
我还为Windows窗体上的按钮和各种其他控件添加了keydown,但我没有收到任何回复.我在函数内部设置了一个断点,看它是否被调用,但该断点永远不会触发?
有任何想法吗?
最优化的是具有触发的通用KeyDown事件(无论当前具有什么焦点的控件),然后调用KeyDown方法.
我编写了一个像这样的方法.但我想这应该进行重构.任何人都可以建议避免使用这多个if语句的最佳方法吗?
private String getMimeType(String fileName){
if(fileName == null) {
return "";
}
if(fileName.endsWith(".pdf")) {
return "application/pdf";
}
if(fileName.endsWith(".doc")) {
return "application/msword";
}
if(fileName.endsWith(".xls")) {
return "application/vnd.ms-excel";
}
if(fileName.endsWith(".xlw")) {
return "application/vnd.ms-excel";
}
if(fileName.endsWith(".ppt")) {
return "application/vnd.ms-powerpoint";
}
if(fileName.endsWith(".mdb")) {
return "application/x-msaccess";
}
if(fileName.endsWith(".rtf")) {
return "application/rtf";
}
if(fileName.endsWith(".txt")) {
return "txt/plain";
}
if(fileName.endsWith(".htm") || fileName.endsWith(".html")) {
return "txt/html";
}
return "txt/plain";
}
Run Code Online (Sandbox Code Playgroud)
我不能在这里使用switch-case,因为我的'条件'是java.lang.String.
我有一个带有退格字符的日志文件(^ H).我正在查看Vim中的文件,很难看到发生了什么.
理想情况下,我希望能够"应用"给定行/范围内的所有^ H,以便我可以看到最终结果.
我宁愿在Vim中逐行进行此操作,但转换整个文件的解决方案总比没有好.
我正在尝试为具有以下格式的多个字段的表单实现泛型函数.
<label id="LblTextCount"></label>
<textarea name="text" onKeyPress="checkLength(this, 512, LblTextCount)">
</textarea>
Run Code Online (Sandbox Code Playgroud)
以下JavaScript:
function checkLength(object, maxlength, label) {
charsleft = (maxlength - object.value.length);
// never allow to exceed the specified limit
if( charsleft < 0 ) {
object.value = object.value.substring(0, maxlength-1);
}
// I'm trying to set the value of charsleft into the label
label.innerText = charsleft;
document.getElementById('LblTextCount').InnerHTML = charsleft;
}
Run Code Online (Sandbox Code Playgroud)
第一部分工作正常,但我无法将charsleft值设置到标签中.我究竟做错了什么?请注意,我正在寻找一种动态方法,而不是将标签名称硬编码到JS函数中.JQuery也会很好:)
HTML
<label id="LblTextCount"></label>
<textarea name="text">
</textarea>
Run Code Online (Sandbox Code Playgroud)
JS
$(document).ready(function() {
$('textarea[name=text]').keypress(function(e) {
checkLength($(this),512,$('#LblTextCount'));
}).focus(function() { …Run Code Online (Sandbox Code Playgroud) alert(window.open('http://www.google.com'));
Run Code Online (Sandbox Code Playgroud)
我试试这个在Firefox上,我得到null,没有概率即.任何想法为什么?
我有如下代码.如果我在我的函数中打开文件$File::Find::name(在这种情况下它是./tmp/tmp.h),它会说"无法打开文件./tmp/tmp.h reason =在temp.pl第36行第98行没有这样的文件或目录".searchFile::Find::find
如果我直接在另一个函数中打开文件,我可以打开该文件.有人能告诉我这种行为的原因吗?我在Windows上使用activeperl,版本是5.6.1.
use warnings;
use strict;
use File::Find;
sub search
{
return unless($File::Find::name =~ /\.h\s*$/);
open (FH,"<", "$File::Find::name") or die "cannot open the file $File::Find::name reason = $!";
print "open success $File::Find::name\n";
close FH;
}
sub fun
{
open (FH,"<", "./tmp/tmp.h") or die "cannot open the file ./tmp/tmp.h reason = $!";
print "open success ./tmp/tmp.h\n";
close FH;
}
find(\&search,".") ;
Run Code Online (Sandbox Code Playgroud) 我有一堆小的PowerShell函数,每个函数都在自己的文件中,我试图将它们组合成一个更大的文件.
通过以下方式使用Windows复制命令的低技术方法:copy /Y /A functions\*.ps1 super.ps1工作正常但是在两个文件连接的情况下插入:
我猜它是显示的换行符(编码的差异),但是如何防止这些字符出现?
如果需要,IE8可以进入IE7模式.jQuery如何检测到这个?具体来说,价值是$.browser.version多少?
我是ReSharper的新手 - 我发现它非常有用,但也注意到我想填补的一些空白.
我想知道
1)你使用的ReSharper插件是什么.
2)你内心极为感兴趣的缺少什么功能?
在我身边:
插件:
A)拼写检查我的XML注释(必须使用Agent Smith)
功能性LACK
a)编号的书签(Deplhi样)(强制使用DPACK)
B)右对齐变量/操作数(如描述这里)
C)可能崩溃的解决方案浏览器中的所有项目(需要再次使用DPACK)