Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

read keystroke in middle of function, not in form

Status
Not open for further replies.

skills

Programmer
Jun 24, 2003
60
US
I need to be able to read in a keystroke in middle of a function. All the help I have gotten before is only helpful if used in the form area. What I have is something like this:

function example()
{
...
...
checkkeystroke()
...
...
checkkeystroke()

...
...
checkkeystroke()
}

and

function checkkeystroke()
{
if (keystroke = esc)
{
esckeyhit = 1;
}
}

Overall, I need to have it so that a function is running, and if esc is pressed, then the function stops. This is the only way I have come up with a solution. Any help would be great.

Thanks,

Skills
 
So, you need to use the onKeyUp / onKeyDown / onKeyPress events. Then you just have a tiny function that only does something if the key capture is an ESC.

You tie these events to your memo field or text field or whatever.

Cheers,


[monkey] Edward [monkey]

"Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!" -- inventor of the cat door
 
First off, I don't know how to use events. Second, would what you just described be used for a form?

Skills
 
Edward's right - you have to capture the keystroke (which can be a big hairy mess in the first place), and then do something like set a global variable for the document which you can then check within your code. I have to say again, though, that the whole event capturing thing can be a big pain in the rear (just letting you know from the start). That being said, which browser or browsers are you coding for? That makes a difference in how you code for the event models (since they vary from browser to browser).
 
Here's an example using IE that continously runs a function until the Escape key is hit while the document has focus.

<script>
var myTimeout;
function loopIt(n){
n++
alert('Looped '+n+' number of times');
myTimeout=setTimeout('loopIt('+n+')',3000);
}
loopIt(0);
</script>
<body onkeypress=&quot;if(event.keyCode==27){clearTimeout(myTimeout);alert('Loop stopped!')}&quot;>

Adam
 
Adam,

What do I do if i don't want it to look for pressed keys until after the form has been submitted?

Skills
 
Okay, what sort of function are you talking about here?

You're not familiar with &quot;events&quot;?

May I encourage you to read up by searching Google for &quot;JavaScript events&quot;

Cheers,


[monkey] Edward [monkey]

&quot;Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!&quot; -- inventor of the cat door
 
I don't understand what purpose that would serve. Most code executed after the form has been submitted will only take a split second to process. The odds that the visitor will be able to hit the escape key within that split second are pretty slim. What exactly are you trying to accomplish? What is the end result you're looking for?

Adam
 
Adam,

Here is the situation...

I have a search engine. It takes up to 3 minutes to finish. The problem is that it works by opening up a window reading the links, and then closing the window. This is done in as fast as the computer can go. Therefore, you can't use anything else until it is done searching since the &quot;top&quot; window keeps changing. So, I have been asked to throw in a quick exit item. So, what I am trying to do is have it so that if the user pushes a key such as escape when the computer is searching, it stops searching and goes directly to where it displays the results.

I hope that helps.

Jonathan
 
Ok, I'm trying to visualize how this works. So I'm guessing it loads several pages sequentially in the popup window and you use JavaScript to read what has been loaded in the popup window and append it to the results in the main window. Am I right so far? If that's right, how do you know when the results of one popup page is finished and it's time to load the next page? Maybe you could post the code you're using so far.

Adam
 
So, another way 'round this is to have the process go faster, such that people aren't annoyed enough to want to interrupt it, right?

Would that solve the problem?

Cheers,


[monkey] Edward [monkey]

&quot;Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!&quot; -- inventor of the cat door
 
Edward and Adam, Here is the code that you desire. Note, The only way I was able to get this to work was to open a window. I tried putting it in a hidden frame, but the page would not load until I got out of the function, which was not an option.


function search(first,find)
{
var j = 0;
var first_window = window.open(first,&quot;first_window&quot;,&quot;width=1,height=1,top=1700,left=1700&quot;);
while (first_window.document.readyState != &quot;complete&quot;)
{
var waiting = 0;
}
while (j < first_window.document.links.length)
{
var pdf_link = &quot;&quot;;
var pdf_link_text = &quot;&quot;;
var a = first_window.document.getElementsByTagName(&quot;a&quot;);

if (a[j].id)

{
//pdf_link = first_window.document.links[j];
pdf_link = a[j].href;
pdf_link_text = a[j].id;
}
else
{
pdf_link_text = &quot;no id value!&quot;;
}
if (pdf_link_text != &quot;no id value!&quot;)
{
compare(pdf_link,pdf_link_text,find);
}

j++;
}
first_window.close();
}

function compare(link,text,desired)
{
var found = text.toUpperCase().indexOf(desired.toUpperCase());
if (found != -1)
{
descriptions[array_index] = text;
listoflinks = listoflinks + link + &quot;,&quot;;
array_index++;
}
}



if (cs2 == true)
{
index_page = &quot;...&quot;;
search(index_page,input)
}
if (cssc == true)
{
index_page = &quot;...&quot;;
search(index_page,input)
}
...
displayresults(input);



I need to be able to go to displayresults() at any time during the code that is in red. I want to be able to do this by pressing a key like esc or end while it is searching.

I hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top