Sign in to follow this  
Followers 0
Morthawt

Need less cpu intensive way to get current key.

20 posts in this topic

Hi.

I am in still coding on my version of what you could call a string hotkey system and am looking for a better yet still relatively simple way to detect the current key pressed on the keyboard that is less cpu intensive. Let me explain how I am currently doing it.

Currently I have a list of all alpha-numeric keys in a 2 dimensional array, first has the hex key and the second has the description. I do a loop that checks _ispressed against all the entries in all of the array entries each time it cycles through.

Is there a way I can pull the raw hex code or something for what ever happens to be the current key press so that I can reduce the cpu usage of my script?

Share this post


Link to post
Share on other sites



Morthawt,

I know that this was mentioned in your last post on this subject. Look at "hotstring". I don't remember where it is, do a search for it. In particular, look at the way the hook passes the key value back to the caller. I believe that using this method (post message) eliminates the whole timing issue (looping fast enough to catch all keys but not so fast that you dominate the CPU).

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

Morthawt,

Are you familiar with "hooking" procedures?


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

Nevermind. The key value is passed to "_Guikeyproc" which calls "evaluatekey".

Do you see that?


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Morthawt,

Are you familiar with "hooking" procedures?

lol absolutely not in the least. I don't get that deep into windows and its atomic structure. I just go with the flow using what I know and learn the best way I can. So far I have made my script totally functional and taken steps to minimize cpu usage but I would like to improve it by still being to check for the strings at any time without needing a control key to start watching all alpha-numeric keys.

If I can find a relatively simple way just to get the current key press from windows, I could just use that information every 100 ms or something rather than having to go checking through the entire alphabet and two different sets of numbers in that same amount of time.

Edited by Morthawt

Share this post


Link to post
Share on other sites

Nevermind. The key value is passed to "_Guikeyproc" which calls "evaluatekey".

Do you see that?

So what would I need to take? I was looking at the _Guikeyproc area but I saw a variable in the middle of that dll call and could not figure it out. From my googling it looked like you needed to feed it a key in order to get information about it, thus made no sense to me.

If you understand all this type of thing, what would be the minimum script I need just to get the current key press? That would be all I need to rewrite/modify my script to hopefully reduce my cpu usage.

Share this post


Link to post
Share on other sites

_Guikeyproc looks like this

Func _GUIKeyProc($hWnd, $Msg, $wParam, $lParam)
    EvaluateKey(Number($wParam))
EndFunc

What DLL call?


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

This $aRet = DllCall('user32.dll', 'int', 'GetKeyNameText', 'int', $lParam, 'str', "", 'int', 256)

I tried using that function you typed but it does not seem to work or do anything. When it comes to dealing with dll's or hooking I know absolutly nothing which is why I have had to make do with using _ispressed.

Could you briefly explain how I can get the current key press into a variable? From there I can code everything I would need, I just need to some how get what ever the current key is.

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Morthawt,

My apologies, I am looking at source that pre-dates the source in the example scripts section. This must have been an earlier version

<snip>

I am sure that the version currently in examples scripts is better.

Edited by Melba23
Code removed

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

Yeah all that source is way above my level of experience. I would literally need a cut down small piece of code that literally just gets the current key press, I could work with that then. I do not understand any of all this. Sorry for being a pain, but could you show me the absolute minimum code I would need to just get the key press information? If I can get that, thats all I need I should be able to use that and just buffer the last few presses and check for the existence of a prior arranged sequence of presses to perform an action myself.

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

No problem. This code was adapted from code originally published by Manadar and Gary Frost.

<snip>

Run it from SCite, type something into a comments section of the code and watch the console...

Good Luck,

kylomas

Edited by Melba23
Code removed

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

Incidentally, you will need to provide a way to exit...

Edit: Comment - I would encourage you to spend some time with "hotstrings". It does EXACTLY what you are doing.

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

Yes I know it does "almost" what I want, I have security features and all sorts of things built into the one I am making. Plus I enjoy "making" rather than just using externally sourced UDF's.

So the source you showed me last, I would need all of that entire thing to get the current keypress? I thought I found only what I needed so I took that into a new file and it did nothing until I had all of the source there.

So there is no simple way to do a "hey windows, whats the current key press?" "Oh its err mmm 0x11" then? You have to use tons of code?

Share this post


Link to post
Share on other sites

Your question is not that simple. You can call "getasynckeystate" at any time in a loop (similar to "ispressed"). You will then need to poll the keyboard iteratively.

The technique used in "hotstrings" posts a message to your dummy gui which you then send to a function (no polling the keyboard). Timing issues are now reduced to "how quickly do you want to respond to a key/set of keys". In a sense you are saying "hey windows, whats the current key press?" "Oh its err mmm 0x11". But you need to code I posted to do that.

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

Ok, if i wanted to call that in a loop, how would I do just that to get the current keypress? I am not in any dire need for a continuous detection like it detecting xxxxxxxxxxxxxxx in a couple miliseconds or anything. Currently my script waits until the current press has stopped and then will look for the next key. I just would like a better way to detect the key to begin with.

So based on what you said, how would I call that in a loop to get the key press? I am thinking as simplistic as possible so I can make use of it.

Share this post


Link to post
Share on other sites

That is the point, you do NOT call this in a loop to get a key press. When a key is pressed a message is posted to your dummy gui. In your case you might collect them in a string (like hotstrings does) and compare them to some array of values/actions (again, like hotstrings). All of the work is done withing the function "evaluatekey" (which, of course, can call other functions, your security for example).

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

#20 ·  Posted (edited)

Morthawt,

In both this thread and your previous one you have strayed too close to prohibited territory. As you quite clearly stated in that other thread:

I know there are certain types of program banned from discussion/help in this forum and although my program is not bad, technically it could relatively easily be reconfigured for negative purposes

You should have taken your own advice. :D

I don't want to get suspensions or bans due to just wanting feedback on my coding style on this one

In that case I suggest you do not start any more threads about detecting the current keypress - the site owner does not want this sort of thing discussed here. ;)

I hope I made myself quite clear. :)

M23

Edited by Melba23
Typo

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this  
Followers 0