Jump to content

Map one key to another?


Vicate
 Share

Recommended Posts

Greetings,

What I am trying to do is very simple: Map one key (such as "S") to another (such as "NUMPAD5")... however I cannot find a more simple way to achieve than this:

If (_IsPressed('53', $dll)) Then
   Send ( "{NUMPAD5 DOWN}")
EndIf
If Not (_IsPressed('53', $dll)) Then
   Send ( "{NUMPAD5 UP}")
EndIf

This works but there has got to be a less redundant way to do this.

HotKeySet seems to only work with functions, otherwise it would be amazingly simple!

Anyway, any help would be appreciated. Thanks!

Edit: It seems the send commands are being continually sent which is compromising the efficiency of the script.

I could add a $Num5 = 'Down'/ 'Up' to only send when it it pressed or not, but again, is there a less redundant way of achieving this?

Edited by Vicate
Link to comment
Share on other sites

this is a combined script from yours and forumer100`s :)

#include <Misc.au3>
HotKeySet("{s}", "_s")
While 1
Sleep(10)
WEnd
Func _s()
If _IsPressed('53')=1 Then Send ( "{NUMPAD5 DOWN}")
Do
  Sleep(10)
Until _IsPressed('53')=0
Send ( "{NUMPAD5 UP}")
EndFunc

edit:

sorry copied the while loop from forumer100

edit 2:

@AdmiralAlkex yep thx for the input

Edited by Blinky
Link to comment
Share on other sites

Replace Sleep(9999999) with Sleep(10), otherwise it'll be awhile before the function gets executed.

No it won't. HotKeySet() is an interrupting function. 10 ms or 10 minutes won't make a difference in that main-loop.

On the other hand, 9999999 is really long and odd. Should be changed anyway.

Link to comment
Share on other sites

"On the other hand, 9999999 is really long and odd. Should be changed anyway."

It is not long and odd.

Maximum sleep time is 2147483647 milliseconds (24 days). From the help file.

Since the script has no other purpose than mapping the key, the higher the value, the less CPU usage.

2147483647 would be the best value, but I was too lasy to type this value. 9999999 seemed enough to me.

If there would be any other code in this script, the while loop would be obsolete.

App: Au3toCmd              UDF: _SingleScript()                             

Link to comment
Share on other sites

  • Moderators

forumer100,

You might be interested to know that we did some testing a while ago and found that pausing the script for some 100 nanoseconds was quite sufficient to keep the CPU load and temperature down, so using Sleep(10) to give a 10 millisecond pause should be quite enough. :)

There is nothing wrong with using a higher value as long as you have nothing else in the loop and you are using OnEvent mode - but it is not a habit I would encourage as it is too likely to cause problems when one of those 2 conditions is not met. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

You might be interested to know that we did some testing a while ago and found that pausing the script for some 100 nanoseconds was quite sufficient to keep the CPU load and temperature down, so using Sleep(10) to give a 10 millisecond pause should be quite enough. :)

So I did some testing just now to prove that there is a difference in using sleeptime 10 or 9999999

Let run this script and see the diiference in runtime between those two values.

#NoTrayIcon
If Not @Compiled Then Exit MsgBox(262144, "", @LF & "Please compile the script." & @LF, 0)
If $CmdLine[0] Then ; this is a subtask
While 1
  Sleep($CmdLine[1])
WEnd
EndIf
$text = ""
$text &= _sleeptimetest(300, 10) ; 300 tasks, 10 milliseconds sleeptime
$text &= _sleeptimetest(300, 9999999) ; 300 tasks, 9999999 milliseconds sleeptime
MsgBox(262144, "Sleeptimetest results", @LF & $text & @LF, 0)
Func _sleeptimetest($taskcount, $sleeptime)
$timer = TimerInit()
For $i = 1 To $taskcount ; build all subtasks
  ShellExecute(@ScriptFullPath, $sleeptime)
Next
$aPid = ProcessList(@ScriptName)
For $i = 1 To $aPid[0][0]
  If $aPid[$i][1] = @AutoItPID Then ContinueLoop
  ProcessClose($aPid[$i][1])
Next
Return TimerDiff($timer) & "   " & $taskcount & "   " & $sleeptime & @LF
EndFunc   ;==>_sleeptimetest

On my PC, the results are:

27 seconds.for sleeptime 10,

21 seconds.for sleeptime 9999999.

This is caused by the excessive timing interupts when usimg small sleeptimes.

If you have a fast performing PC, try it with a taskcount of 1000 and you will see a great difference.

There are many eventdriven sample script in this forum, with have the following lines as last lines of the main script:

while 1
  sleep(10)
wend

Since there is no chance to leave this loop, it is the best solution to enter a high sleeptime.

Then there will be no timed interupt disturbing the other processes.

The eventdriven functions (hotkeys and tray) will run asyncroniosly. not waiting for the sleep() to expire.

On the other hand, when not using eventdriven mode. you will never use a neverending loop like above.

App: Au3toCmd              UDF: _SingleScript()                             

Link to comment
Share on other sites

  • Moderators

forumer100,

My comments above were based on CPU loading not timing, but your results are very interesting and worth keeping in mind. Thank you for taking the time to do the research and also letting us know about it. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

@forumer100

You can't argue that 9 is a odd number.

On my PC, the results are:

27 seconds.for sleeptime 10,

21 seconds.for sleeptime 9999999

I'm not sure those results can be trusted.

I got:

6491.93972473025 300 10

6274.87610812973 300 9999999

And the results fluctuate wildly. I run it a few times and got both better and worse.

Upping the taskcount to 511 (apparantly max ProcessList() can handle) didn't significantly change anything.

31286.8068539954 511 10

27493.0119172682 511 9999999

That was from yesterday. Today I get results like:

20405.9340395888 511 10

19211.1125300205 511 9999999

And

17706.6637471937 511 10

17225.6584927448 511 9999999

A 10 second difference. Nothing was changed. In fact, I didn't turn my PC off tonight, it's still running from when I got the earlier numbers.

But to be serious again, look at this:

16534.902058108 511 10

15244.8923780673 511 1000

14864.3502235083 511 10000

14905.5738240136 511 60000

14669.8804963747 511 100000

14716.847366782 511 9999999

1 second is enough to take away half the "advantage" from 9999999, and at 10 seconds it's practically gone.

Some funny runs:

17396.5232570957 511 10

18299.180720395 511 9999999

16958.8375397638 511 10

17366.7506567308 511 1000

17772.9488343291 511 10000

17655.7055918348 511 60000

17622.4985949785 511 100000

17912.3853921139 511 9999999

TL;DR: 9999999 looks fugly and provide little to no benefits. This test you have written is very inconsistent. Your PC is slooow.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...