[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

Convoluted key events

JT

4/3/2016 4:16:00 PM

I try to grip and understand javascript key events, but i fail miserably.
It may seem that i try to do a double RNG, but i want to generate a steady flow and take the value that is generated into myval at the time i press the key.

However not even my function call to the random function work, a hint or an example, there certainly something i do not understand about the listeners function. I would like it to loop and wait for 1000 keypress generating 1000 values of 0 and 1.

But not just 1000 random values, i want the function to be called in a loop generating values but i want the value generated as i press the key.

I remember doing this in Basic it was very simple.
13 Answers

JT

4/3/2016 4:20:00 PM

0

Den söndag 3 april 2016 kl. 18:15:41 UTC+2 skrev jonas.t...@gmail.com:
> I try to grip and understand javascript key events, but i fail miserably.
> It may seem that i try to do a double RNG, but i want to generate a steady flow and take the value that is generated into myval at the time i press the key.
>
> However not even my function call to the random function work, a hint or an example, there certainly something i do not understand about the listeners function. I would like it to loop and wait for 1000 keypress generating 1000 values of 0 and 1.
>
> But not just 1000 random values, i want the function to be called in a loop generating values but i want the value generated as i press the key.
>
> I remember doing this in Basic it was very simple.

<script>
document.onkeyup = getKey;

function getKey() {
// If the users hits 'a', stop loopcode from running.
if(event.keyCode == 65){
window.clearInterval(interval);
document.write("test ",myval);
};
}

function random (low, high) {
binary=Math.floor(Math.random() * (max - min + 1)) + min;
return binary;
}

myval=0;
var interval = setInterval(function() {
// loopcode here
low=0;high=1;
myval=random(low,high);
}, 0);

</script>

JT

4/3/2016 4:33:00 PM

0

Den söndag 3 april 2016 kl. 18:19:40 UTC+2 skrev jonas.t...@gmail.com:
> Den söndag 3 april 2016 kl. 18:15:41 UTC+2 skrev jonas.t...@gmail.com:
> > I try to grip and understand javascript key events, but i fail miserably.
> > It may seem that i try to do a double RNG, but i want to generate a steady flow and take the value that is generated into myval at the time i press the key.
> >
> > However not even my function call to the random function work, a hint or an example, there certainly something i do not understand about the listeners function. I would like it to loop and wait for 1000 keypress generating 1000 values of 0 and 1.
> >
> > But not just 1000 random values, i want the function to be called in a loop generating values but i want the value generated as i press the key.
> >
> > I remember doing this in Basic it was very simple.
>
> <script>
> document.onkeyup = getKey;
>
> function getKey() {
> // If the users hits 'a', stop loopcode from running.
> if(event.keyCode == 65){
> window.clearInterval(interval);
> document.write("test ",myval);
> };
> }
>
> function random (low, high) {
> binary=Math.floor(Math.random() * (max - min + 1)) + min;
> return binary;
> }
>
> myval=0;
> var interval = setInterval(function() {
> // loopcode here
> low=0;high=1;
> myval=random(low,high);
> }, 0);
>
> </script>

And why " document.onkeyup = getKey;" isn't "document.onkeyup = getKey();" is also weird?

JT

4/3/2016 4:53:00 PM

0

Den söndag 3 april 2016 kl. 18:32:38 UTC+2 skrev jonas.t...@gmail.com:
> Den söndag 3 april 2016 kl. 18:19:40 UTC+2 skrev jonas.t...@gmail.com:
> > Den söndag 3 april 2016 kl. 18:15:41 UTC+2 skrev jonas.t...@gmail.com:
> > > I try to grip and understand javascript key events, but i fail miserably.
> > > It may seem that i try to do a double RNG, but i want to generate a steady flow and take the value that is generated into myval at the time i press the key.
> > >
> > > However not even my function call to the random function work, a hint or an example, there certainly something i do not understand about the listeners function. I would like it to loop and wait for 1000 keypress generating 1000 values of 0 and 1.
> > >
> > > But not just 1000 random values, i want the function to be called in a loop generating values but i want the value generated as i press the key.
> > >
> > > I remember doing this in Basic it was very simple.
> >
> > <script>
> > document.onkeyup = getKey;
> >
> > function getKey() {
> > // If the users hits 'a', stop loopcode from running.
> > if(event.keyCode == 65){
> > window.clearInterval(interval);
> > document.write("test ",myval);
> > };
> > }
> >
> > function random (low, high) {
> > binary=Math.floor(Math.random() * (max - min + 1)) + min;
> > return binary;
> > }
> >
> > myval=0;
> > var interval = setInterval(function() {
> > // loopcode here
> > low=0;high=1;
> > myval=random(low,high);
> > }, 0);
> >
> > </script>
>
> And why " document.onkeyup = getKey;" isn't "document.onkeyup = getKey();" is also weird?

Found some mismatch...
<script>
document.onkeyup = getKey;

function getKey() {
// If the users hits 'a', stop loopcode from running.
if(event.keyCode == 65){
window.clearInterval(interval);
document.write("test ",myval);
};
}

function random (min, max) {
binary=Math.floor(Math.random() * (max - min + 1)) + min;
return binary;
}

myval=0;
for(x=0;x<1000;x++){
var interval = setInterval(function() {
// loopcode here
low=0;high=1;
myval=random(low,high);
}, 1);
}

JT

4/3/2016 5:16:00 PM

0

Den söndag 3 april 2016 kl. 18:52:55 UTC+2 skrev jonas.t...@gmail.com:
> Den söndag 3 april 2016 kl. 18:32:38 UTC+2 skrev jonas.t...@gmail.com:
> > Den söndag 3 april 2016 kl. 18:19:40 UTC+2 skrev jonas.t...@gmail.com:
> > > Den söndag 3 april 2016 kl. 18:15:41 UTC+2 skrev jonas.t...@gmail.com:
> > > > I try to grip and understand javascript key events, but i fail miserably.
> > > > It may seem that i try to do a double RNG, but i want to generate a steady flow and take the value that is generated into myval at the time i press the key.
> > > >
> > > > However not even my function call to the random function work, a hint or an example, there certainly something i do not understand about the listeners function. I would like it to loop and wait for 1000 keypress generating 1000 values of 0 and 1.
> > > >
> > > > But not just 1000 random values, i want the function to be called in a loop generating values but i want the value generated as i press the key.
> > > >
> > > > I remember doing this in Basic it was very simple.
> > >
> > > <script>
> > > document.onkeyup = getKey;
> > >
> > > function getKey() {
> > > // If the users hits 'a', stop loopcode from running.
> > > if(event.keyCode == 65){
> > > window.clearInterval(interval);
> > > document.write("test ",myval);
> > > };
> > > }
> > >
> > > function random (low, high) {
> > > binary=Math.floor(Math.random() * (max - min + 1)) + min;
> > > return binary;
> > > }
> > >
> > > myval=0;
> > > var interval = setInterval(function() {
> > > // loopcode here
> > > low=0;high=1;
> > > myval=random(low,high);
> > > }, 0);
> > >
> > > </script>
> >
> > And why " document.onkeyup = getKey;" isn't "document.onkeyup = getKey();" is also weird?
>
> Found some mismatch...
> <script>
> document.onkeyup = getKey;
>
> function getKey() {
> // If the users hits 'a', stop loopcode from running.
> if(event.keyCode == 65){
> window.clearInterval(interval);
> document.write("test ",myval);
> };
> }
>
> function random (min, max) {
> binary=Math.floor(Math.random() * (max - min + 1)) + min;
> return binary;
> }
>
> myval=0;
> for(x=0;x<1000;x++){
> var interval = setInterval(function() {
> // loopcode here
> low=0;high=1;
> myval=random(low,high);
> }, 1);
> }

?
<script>
document.onkeyup = getKey;

function getKey() {
// If the users hits 'a', stop loopcode from running.
if(event.keyCode == 65){
window.clearInterval(interval);
document.esp.bin.value+=myval;
// document.write("jonas");
};
}

function random (min, max) {
binary=Math.floor(Math.random() * (max - min + 1)) + min;
return binary;
}

interval=1;
myval=0;
var interval = setInterval(function() {
// loopcode here
low=0;high=1;
myval=random(low,high);
}, 1);



</script>
<HTML><BODY>

JT

4/3/2016 5:54:00 PM

0

Den söndag 3 april 2016 kl. 19:16:28 UTC+2 skrev jonas.t...@gmail.com:
> Den söndag 3 april 2016 kl. 18:52:55 UTC+2 skrev jonas.t...@gmail.com:
> > Den söndag 3 april 2016 kl. 18:32:38 UTC+2 skrev jonas.t...@gmail.com:
> > > Den söndag 3 april 2016 kl. 18:19:40 UTC+2 skrev jonas.t...@gmail.com:
> > > > Den söndag 3 april 2016 kl. 18:15:41 UTC+2 skrev jonas.t...@gmail..com:
> > > > > I try to grip and understand javascript key events, but i fail miserably.
> > > > > It may seem that i try to do a double RNG, but i want to generate a steady flow and take the value that is generated into myval at the time i press the key.
> > > > >
> > > > > However not even my function call to the random function work, a hint or an example, there certainly something i do not understand about the listeners function. I would like it to loop and wait for 1000 keypress generating 1000 values of 0 and 1.
> > > > >
> > > > > But not just 1000 random values, i want the function to be called in a loop generating values but i want the value generated as i press the key.
> > > > >
> > > > > I remember doing this in Basic it was very simple.
> > > >
> > > > <script>
> > > > document.onkeyup = getKey;
> > > >
> > > > function getKey() {
> > > > // If the users hits 'a', stop loopcode from running.
> > > > if(event.keyCode == 65){
> > > > window.clearInterval(interval);
> > > > document.write("test ",myval);
> > > > };
> > > > }
> > > >
> > > > function random (low, high) {
> > > > binary=Math.floor(Math.random() * (max - min + 1)) + min;
> > > > return binary;
> > > > }
> > > >
> > > > myval=0;
> > > > var interval = setInterval(function() {
> > > > // loopcode here
> > > > low=0;high=1;
> > > > myval=random(low,high);
> > > > }, 0);
> > > >
> > > > </script>
> > >
> > > And why " document.onkeyup = getKey;" isn't "document.onkeyup = getKey();" is also weird?
> >
> > Found some mismatch...
> > <script>
> > document.onkeyup = getKey;
> >
> > function getKey() {
> > // If the users hits 'a', stop loopcode from running.
> > if(event.keyCode == 65){
> > window.clearInterval(interval);
> > document.write("test ",myval);
> > };
> > }
> >
> > function random (min, max) {
> > binary=Math.floor(Math.random() * (max - min + 1)) + min;
> > return binary;
> > }
> >
> > myval=0;
> > for(x=0;x<1000;x++){
> > var interval = setInterval(function() {
> > // loopcode here
> > low=0;high=1;
> > myval=random(low,high);
> > }, 1);
> > }
>
> ?
> <script>
> document.onkeyup = getKey;
>
> function getKey() {
> // If the users hits 'a', stop loopcode from running.
> if(event.keyCode == 65){
> window.clearInterval(interval);
> document.esp.bin.value+=myval;
> // document.write("jonas");
> };
> }
>
> function random (min, max) {
> binary=Math.floor(Math.random() * (max - min + 1)) + min;
> return binary;
> }
>
> interval=1;
> myval=0;
> var interval = setInterval(function() {
> // loopcode here
> low=0;high=1;
> myval=random(low,high);
> }, 1);
>
>
>
> </script>
> <HTML><BODY>

I got something working in chrome, firefox complains event not defined.
But i do not think what come out is random?


<script>
document.onkeyup = getKey;

function getKey() {
// If the users hits 'a', stop loopcode from running.
if(event.keyCode == 65){
window.clearInterval(interval);
document.esp.bin.value+=binary.slice(-1);
// document.write("jonas");
};
}

interval=1;
min=0;max=1;
binary="";
for (x=0;x<1000;x++){
interval = setInterval(function() {
// loopcode here
binary+=Math.floor(Math.random() * (max - min + 1)) + min;
}, 0);
}


</script>
//<HTML><BODY>
//<FORM NAME="esp">
//GENERATED VALUES<br>
//<TEXTAREA NAME="bin" COLS=100 ROWS=10></TEXTAREA></FORM>
//</BODY></HTML>

JT

4/3/2016 6:17:00 PM

0

Den söndag 3 april 2016 kl. 19:53:57 UTC+2 skrev jonas.t...@gmail.com:
> Den söndag 3 april 2016 kl. 19:16:28 UTC+2 skrev jonas.t...@gmail.com:
> > Den söndag 3 april 2016 kl. 18:52:55 UTC+2 skrev jonas.t...@gmail.com:
> > > Den söndag 3 april 2016 kl. 18:32:38 UTC+2 skrev jonas.t...@gmail.com:
> > > > Den söndag 3 april 2016 kl. 18:19:40 UTC+2 skrev jonas.t...@gmail..com:
> > > > > Den söndag 3 april 2016 kl. 18:15:41 UTC+2 skrev jonas.t...@gmail.com:
> > > > > > I try to grip and understand javascript key events, but i fail miserably.
> > > > > > It may seem that i try to do a double RNG, but i want to generate a steady flow and take the value that is generated into myval at the time i press the key.
> > > > > >
> > > > > > However not even my function call to the random function work, a hint or an example, there certainly something i do not understand about the listeners function. I would like it to loop and wait for 1000 keypress generating 1000 values of 0 and 1.
> > > > > >
> > > > > > But not just 1000 random values, i want the function to be called in a loop generating values but i want the value generated as i press the key.
> > > > > >
> > > > > > I remember doing this in Basic it was very simple.
> > > > >
> > > > > <script>
> > > > > document.onkeyup = getKey;
> > > > >
> > > > > function getKey() {
> > > > > // If the users hits 'a', stop loopcode from running.
> > > > > if(event.keyCode == 65){
> > > > > window.clearInterval(interval);
> > > > > document.write("test ",myval);
> > > > > };
> > > > > }
> > > > >
> > > > > function random (low, high) {
> > > > > binary=Math.floor(Math.random() * (max - min + 1)) + min;
> > > > > return binary;
> > > > > }
> > > > >
> > > > > myval=0;
> > > > > var interval = setInterval(function() {
> > > > > // loopcode here
> > > > > low=0;high=1;
> > > > > myval=random(low,high);
> > > > > }, 0);
> > > > >
> > > > > </script>
> > > >
> > > > And why " document.onkeyup = getKey;" isn't "document.onkeyup = getKey();" is also weird?
> > >
> > > Found some mismatch...
> > > <script>
> > > document.onkeyup = getKey;
> > >
> > > function getKey() {
> > > // If the users hits 'a', stop loopcode from running.
> > > if(event.keyCode == 65){
> > > window.clearInterval(interval);
> > > document.write("test ",myval);
> > > };
> > > }
> > >
> > > function random (min, max) {
> > > binary=Math.floor(Math.random() * (max - min + 1)) + min;
> > > return binary;
> > > }
> > >
> > > myval=0;
> > > for(x=0;x<1000;x++){
> > > var interval = setInterval(function() {
> > > // loopcode here
> > > low=0;high=1;
> > > myval=random(low,high);
> > > }, 1);
> > > }
> >
> > ?
> > <script>
> > document.onkeyup = getKey;
> >
> > function getKey() {
> > // If the users hits 'a', stop loopcode from running.
> > if(event.keyCode == 65){
> > window.clearInterval(interval);
> > document.esp.bin.value+=myval;
> > // document.write("jonas");
> > };
> > }
> >
> > function random (min, max) {
> > binary=Math.floor(Math.random() * (max - min + 1)) + min;
> > return binary;
> > }
> >
> > interval=1;
> > myval=0;
> > var interval = setInterval(function() {
> > // loopcode here
> > low=0;high=1;
> > myval=random(low,high);
> > }, 1);
> >
> >
> >
> > </script>
> > <HTML><BODY>
>
> I got something working in chrome, firefox complains event not defined.
> But i do not think what come out is random?
>
>
> <script>
> document.onkeyup = getKey;
>
> function getKey() {
> // If the users hits 'a', stop loopcode from running.
> if(event.keyCode == 65){
> window.clearInterval(interval);
> document.esp.bin.value+=binary.slice(-1);
> // document.write("jonas");
> };
> }
>
> interval=1;
> min=0;max=1;
> binary="";
> for (x=0;x<1000;x++){
> interval = setInterval(function() {
> // loopcode here
> binary+=Math.floor(Math.random() * (max - min + 1)) + min;
> }, 0);
> }
>
>
> </script>
> //<HTML><BODY>
> //<FORM NAME="esp">
> //GENERATED VALUES<br>
> //<TEXTAREA NAME="bin" COLS=100 ROWS=10></TEXTAREA></FORM>
> //</BODY></HTML>

This really can't be random 1110001101111101100100001011111111110101000010110011100011111110110100100111001000000011010 and i did move binary=""; into the loop.

Michael Haufe (\"TNO\")

4/3/2016 7:21:00 PM

0

On Sunday, April 3, 2016 at 12:53:57 PM UTC-5, jonas.t...@gmail.com wrote:
> [...]

For once in your life, use jshint.com on your code before asking a question.

JT

4/3/2016 8:49:00 PM

0

Den söndag 3 april 2016 kl. 21:20:49 UTC+2 skrev Michael Haufe (TNO):
> On Sunday, April 3, 2016 at 12:53:57 PM UTC-5, jonas.t...@gmail.com wrote:
> > [...]
>
> For once in your life, use jshint.com on your code before asking a question.

I did but apparently the language become so convoluted i really did not understand that the thing was a funtioncall looking at the code i still don't get it.

interval = setInterval(function() { // loopcode here }, 0);



Could you tell me what this code actually mean? I mean what is function() setInterval i do recognize as some timer one use, but i do not get what is actually returned?? what is the value that interval get 0 ms?

To me the code make no sense, well the syntax i do not get it.
I can see clearInterval reset the setInterval, is setInterval and clearinterval somehow the interval of the key listener?
It isn't exactly obvious from the code that the interval even related to the keylistener?

Lets say that i want to listen to the key a 1000 times should i call setInterval a 1000 times. It all seem convoluted and strange to me?

I can explain waht i want todo in a single sentence and i can code it using basic in four lines.
I want to run the RNG in a loop generating values in a buffer called binary or "whatever" when i press space or "any key" i want to print what is in buffer?

Is it just my poor understanding of the language, or is there something incomprehensible convoluted that make it hard to express statements using the language builtin eventhandler.

How many lines does it take todo what i describe above?
Pseudocode
myRandomVal(){
interval=0;
start.keyUP(A,interval)
while(keyUP!=32){
random=Math.floor(Math.random() * (max - min + 1)) + min;
if(keyUP=32) {return random};
}
//clear.keyUP(A);
stop.keyUP(A);
}

for (var i=1;i<1000;i++){
out=myRandomVal();
document.write(out);
}

I mean what is the reason to make the code incomprehensible "like my failed attempt above".
Feel free to show me how it is done in Javascript, and if the code is cumbersome and convoluted please tell me why.

JT

4/3/2016 8:59:00 PM

0

Den söndag 3 april 2016 kl. 22:49:09 UTC+2 skrev jonas.t...@gmail.com:
> Den söndag 3 april 2016 kl. 21:20:49 UTC+2 skrev Michael Haufe (TNO):
> > On Sunday, April 3, 2016 at 12:53:57 PM UTC-5, jonas.t...@gmail.com wrote:
> > > [...]
> >
> > For once in your life, use jshint.com on your code before asking a question.
>
> I did but apparently the language become so convoluted i really did not understand that the thing was a funtioncall looking at the code i still don't get it.
>
> interval = setInterval(function() { // loopcode here }, 0);
>
>
>
> Could you tell me what this code actually mean? I mean what is function() setInterval i do recognize as some timer one use, but i do not get what is actually returned?? what is the value that interval get 0 ms?
>
> To me the code make no sense, well the syntax i do not get it.
> I can see clearInterval reset the setInterval, is setInterval and clearinterval somehow the interval of the key listener?
> It isn't exactly obvious from the code that the interval even related to the keylistener?
>
> Lets say that i want to listen to the key a 1000 times should i call setInterval a 1000 times. It all seem convoluted and strange to me?
>
> I can explain waht i want todo in a single sentence and i can code it using basic in four lines.
> I want to run the RNG in a loop generating values in a buffer called binary or "whatever" when i press space or "any key" i want to print what is in buffer?
>
> Is it just my poor understanding of the language, or is there something incomprehensible convoluted that make it hard to express statements using the language builtin eventhandler.
>
> How many lines does it take todo what i describe above?
> Pseudocode
> myRandomVal(){
> interval=0;
> start.keyUP(A,interval)
> while(keyUP!=32){
> random=Math.floor(Math.random() * (max - min + 1)) + min;
> if(keyUP=32) {return random};
> }
> //clear.keyUP(A);
> stop.keyUP(A);
> }
>
> for (var i=1;i<1000;i++){
> out=myRandomVal();
> document.write(out);
> }
>
> I mean what is the reason to make the code incomprehensible "like my failed attempt above".
> Feel free to show me how it is done in Javascript, and if the code is cumbersome and convoluted please tell me why.

And even my code seem convoluted naming the keyUP A but i can see it is useful if differentlisteners assigned to different keyranges.

JT

4/4/2016 4:45:00 AM

0

Den söndag 3 april 2016 kl. 22:59:34 UTC+2 skrev jonas.t...@gmail.com:
> Den söndag 3 april 2016 kl. 22:49:09 UTC+2 skrev jonas.t...@gmail.com:
> > Den söndag 3 april 2016 kl. 21:20:49 UTC+2 skrev Michael Haufe (TNO):
> > > On Sunday, April 3, 2016 at 12:53:57 PM UTC-5, jonas.t...@gmail.com wrote:
> > > > [...]
> > >
> > > For once in your life, use jshint.com on your code before asking a question.
> >
> > I did but apparently the language become so convoluted i really did not understand that the thing was a funtioncall looking at the code i still don't get it.
> >
> > interval = setInterval(function() { // loopcode here }, 0);
> >
> >
> >
> > Could you tell me what this code actually mean? I mean what is function() setInterval i do recognize as some timer one use, but i do not get what is actually returned?? what is the value that interval get 0 ms?
> >
> > To me the code make no sense, well the syntax i do not get it.
> > I can see clearInterval reset the setInterval, is setInterval and clearinterval somehow the interval of the key listener?
> > It isn't exactly obvious from the code that the interval even related to the keylistener?
> >
> > Lets say that i want to listen to the key a 1000 times should i call setInterval a 1000 times. It all seem convoluted and strange to me?
> >
> > I can explain waht i want todo in a single sentence and i can code it using basic in four lines.
> > I want to run the RNG in a loop generating values in a buffer called binary or "whatever" when i press space or "any key" i want to print what is in buffer?
> >
> > Is it just my poor understanding of the language, or is there something incomprehensible convoluted that make it hard to express statements using the language builtin eventhandler.
> >
> > How many lines does it take todo what i describe above?
> > Pseudocode
> > myRandomVal(){
> > interval=0;
> > start.keyUP(A,interval)
> > while(keyUP!=32){
> > random=Math.floor(Math.random() * (max - min + 1)) + min;
> > if(keyUP=32) {return random};
> > }
> > //clear.keyUP(A);
> > stop.keyUP(A);
> > }
> >
> > for (var i=1;i<1000;i++){
> > out=myRandomVal();
> > document.write(out);
> > }
> >
> > I mean what is the reason to make the code incomprehensible "like my failed attempt above".
> > Feel free to show me how it is done in Javascript, and if the code is cumbersome and convoluted please tell me why.
>
> And even my code seem convoluted naming the keyUP A but i can see it is useful if differentlisteners assigned to different keyranges.

Yes i know sloppy
Pseudocode

function myRandomVal(){
interval=0;
random=0;
start.keyUP(A,interval)
while(keyUP(A)!=32){
if(random==0) random=1 elseif (random==1) random=0;

if(keyUP==32) {return random};
}
//clear.keyUP(A);
stop.keyUP(A);
}

for (var i=1;i<1000;i++){
out=myRandomVal();
document.write(out);
}