When the cursor is moved while the "A" key is held, I want a variable flag to be set.
$(document).on("mousemove", function () {
$(document).keydown(function(e) {
if(e.which == 65){
var $mLeft = '3px',
$mTop = event.pageY - $sidebarRightDisp;
} else {
var $mLeft = event.pageX - $sidebarLeftDisp,
$mTop = event.pageY - $sidebarRightDisp;
}
});
var $boxStyleAct = 'left:' + $mLeft + 'px;top:' + $mTop + 'px' + ';';
var $boxAct = '<li style="' + $boxStyleAct + '"></li>';
$(".boxes").append($boxAct);
});
By the way, I really can't put my "// do something with x" inside the keydown function.
Thank you very much.
Your x
variable is defined within the scope of the if statements. You could move it out. You're also adding an event listener on mousemove which isn't good.
You could instead do something like this.
var aPressed = false;
var keyPressed = false;
var sidebarRightDisp = 5;
var sidebarLeftDisp = 5;
$(document).mousemove(function(event) {
if (!keyPressed)
return;
var mLeft, mTop;
if (aPressed) {
mLeft = '3px';
mTop = event.pageY - sidebarRightDisp;
} else {
mLeft = event.pageX - sidebarLeftDisp;
mTop = event.pageY - sidebarRightDisp;
}
var boxStyleAct = 'left:' + mLeft + 'px;top:' + mTop + 'px' + ';';
var boxAct = '<li style="' + boxStyleAct + '"></li>';
$(".boxes").append(boxAct);
}).keydown(function(e) {
keyPressed = true;
aPressed = e.which == 65;
}).keyup(function(e) {
keyPressed = false;
aPressed = false;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="boxes">
</div>
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments