Keydown while mouseover and variables in multiple functions

Charles-François St-Cyr

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.

H77

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.

edited at
0

Comments

0 comments
Login to comment

Related

calling multiple functions in @keydown vue3 error

i want to make multiple mouseover functions with minimum codes

Multiple integration with functions of variables in the limits

multiple functions in summrise_each() with multiple variables

VBA autofill functions/variables while typing code

Beeping while KeyDown is triggered

Keydown while execution of a function

javascript mouseover and out functions

Conflicting hover/mouseover functions

Declaring functions and variables multiple times in C++

Vuejs have multiple components sharing variables and functions

How to pass multiple variables and functions to chroot envrionment?

Inheriting variables from multiple class functions with super()

Python Mocking / Patching multiple nested functions / variables

For and while functions, repetition structure with multiple termination conditions

Chain multiple functions in Scala while a condition holds

storing values in functions while calling multiple times

Creating while loop for multiple functions call

While Loop to check multiple variables are equal

How to get multiple variables from a While Statement?

Problem while defining conditions for multiple variables in C

Swap Multiple Images on mouseover

Mouseover and mouseout for multiple element

summarize_at for multiple variables and multiple functions with different arguments in R?

Calling multiple variables from a function to multiple other functions

mouseover and mouseout functions in dropdown menu

how to pass variables between functions in a class, while defining them as an object?

Multiple keys with WPF KeyDown event

Reusing KeyDown event multiple times