Need Help Finishing a Macros Keyboard for Math

Announcements, questions
Post Reply
Rwelk137
Posts: 5
Joined: 25 Jan 2018, 07:46

Need Help Finishing a Macros Keyboard for Math

Post by Rwelk137 » 25 Jan 2018, 08:34

So I've always found it super annoying that the default keyboard has never been optimized well for typing out math equations for anything past pre-algebra. In order to use parentheses or exponents you always have to first hit shift. Same with adding anything together, because + is the second function of the = key. So when I hit college, I got a cheap numeric keyboard and spent a weeknight plugging in macros for HIDmacros. Then it just stopped working. Maybe it was a computer update. I don't know, and don't particularly care. Now I've been browsing forum threads learning how to use Lua, and rebuilding my macros. Everything has been working almost great. There's just a few keys left that I gotta nail down, and everything will be good. This is my script so far:

Code: Select all

-- assign logical name to my external keyboard
lmc_device_set_name('MathCros', '2F1C1E94');

-- define callback for whole device
lmc_set_handler('MathCros',function(button, direction)
  if (direction == 1) then return end  -- ignore down
  if     (button == 97) then lmc_send_keys('sec(')
  elseif (button == 98) then lmc_send_keys('csc(')
  elseif (button == 99) then lmc_send_keys('cot(')
  elseif (button == 100) then lmc_send_keys('sin(')
  elseif (button == 101) then lmc_send_keys('cos(')
  elseif (button == 102) then lmc_send_keys('tan(')
  elseif (button == 103) then lmc_send_keys('log(')
  elseif (button == 104) then lmc_send_keys('ln(')
  elseif (button == 105) then lmc_send_keys('pi')
  elseif (button == 13) then lmc_send_keys('/')
  
  elseif (button == 109) then lmc_send_keys('^{ }')
  elseif (button == 107) then lmc_send_keys('\sqrt{ }')
  elseif (button == 111) then lmc_send_keys('(')
  elseif (button == 106) then lmc_send_keys(')')

  else print('Not yet assigned: ' .. button)
  end
end)
Running down the list, everything seems pretty normal. A lot of sines, cosines, logarithms, and stuff. It's at the line for the buttons 109 onward that I need help with. The first key is supposed to be a carrot for exponents. The next one is for typing out square roots. Then the final two are for opened and closed parentheses, respectively. If anybody has any idea of how I can make those keys work, it would be great. Preferably even without the closed bracket ( } ), but if that's not doable, I understand. I'm a real noob at coding. :oops:
Last edited by Rwelk137 on 25 Jan 2018, 08:50, edited 1 time in total.

Rwelk137
Posts: 5
Joined: 25 Jan 2018, 07:46

Re: Need Help Finishing a Macros Keyboard for Math

Post by Rwelk137 » 25 Jan 2018, 08:38

I'm also unsure of the parentheses for all the other keys. When I try the keys in Word and Notepad, the letters are spit out without the ( , and the final two keys do nothing as well. I'm well aware that in most, if not all, coding languages, every sort of bracket or parenthesis needs a buddy at the end, or everything can break. So if those last two macros are undoable, I understand. I'm really trying to get the exponents and square roots to work, 'cause tht's what really started me working on this project. Once again, thanks a lot for all the help anybody can give.

admin
Site Admin
Posts: 735
Joined: 01 Nov 2010, 13:00
Location: Prague, Czech republic
Contact:

Re: Need Help Finishing a Macros Keyboard for Math

Post by admin » 25 Jan 2018, 09:41

The problem is that ^ is used as Ctrl modifier and {} for special key names.
For this kind of keys I would go for lmc_send_input.
Have a look at http://www.hidmacros.eu/forum/viewtopic.php?f=12&t=475 and try e.g.

Code: Select all

lmc_send_input(16, 0, 0) -- press shift
lmc_send_input(54, 0, 0) -- press 6
lmc_send_input(54, 0, 2) -- release 6
lmc_send_input(16, 0, 2) -- release shift
to send ^

For complete list of vk_codes even use that MSDN link or google "Virtual key codes", e.g. http://cherrytree.at/misc/vk.htm
Petr Medek
LUAmacros author

Rwelk137
Posts: 5
Joined: 25 Jan 2018, 07:46

Re: Need Help Finishing a Macros Keyboard for Math

Post by Rwelk137 » 25 Jan 2018, 18:48

Thanks a lot. Turns out, the homework software I'm using only requires me to type sqrt without the \ and brackets to work. Everything else went smoothly. I can automatically call both parentheses, and exponentiate. Here's the code for anybody who wants to use it for themselves if they stumble upon this thread.

Code: Select all

-- assign logical name to macro keyboard
lmc_assign_keyboard('MACROS');

-- define callback for whole device
lmc_set_handler('MACROS',function(button, direction)
  if (direction == 1) then return end  -- ignore down
  if     (button == 97) then lmc_send_keys('sec(')
  elseif (button == 98) then lmc_send_keys('csc(')
  elseif (button == 99) then lmc_send_keys('cot(')
  elseif (button == 100) then lmc_send_keys('sin(')
  elseif (button == 101) then lmc_send_keys('cos(')
  elseif (button == 102) then lmc_send_keys('tan(')
  elseif (button == 103) then lmc_send_keys('log(')
  elseif (button == 104) then lmc_send_keys('ln(')
  elseif (button == 105) then lmc_send_keys('pi')
  elseif (button == 13) then lmc_send_keys('/')
  elseif (button == 107) then lmc_send_keys('sqrt')

  elseif (button == 109) then
    lmc_send_input(16, 0, 0) -- press shift
    lmc_send_input(54, 0, 0) -- press 6
    lmc_send_input(54, 0, 2) -- release 6
    lmc_send_input(16, 0, 2) -- release shift, types ^
  elseif (button == 111) then
    lmc_send_input(16, 0, 0) -- press shift
    lmc_send_input(57, 0, 0) -- press 9
    lmc_send_input(57, 0, 2) -- release 9
    lmc_send_input(16, 0, 2) -- release shift, types (
  elseif (button == 106) then
    lmc_send_input(16, 0, 0) -- press shift
    lmc_send_input(48, 0, 0) -- press 0
    lmc_send_input(48, 0, 2) -- release 0
    lmc_send_input(16, 0, 2) -- release shift, types )

  else print('Not yet assigned: ' .. button)
  end

  lmc.minimizeToTray = true
end)
The last little bit just minimizes the program to the tray as per the instructions here: http://www.hidmacros.eu/forum/viewtopic.php?f=12&t=270 It was something that I really liked from HIDMacros, and I'm glad I can use it with Lua too.

There are a few last quality of life things I want to work on, but this is the code that as far as I'm aware, anybody with a cheap numpad can use. Thanks to u/Trit for their attempt that I could piggyback off of (http://www.hidmacros.eu/forum/viewtopic.php?f=10&t=574), and most of all, thanks to Petr for not only helping me out here, but also for making such a great program! :D

admin
Site Admin
Posts: 735
Joined: 01 Nov 2010, 13:00
Location: Prague, Czech republic
Contact:

Re: Need Help Finishing a Macros Keyboard for Math

Post by admin » 26 Jan 2018, 09:46

Just small tuning:
lmc.minimizeToTray = true
sets the flag so you can set it once at the beginning, e.g. next to lmc_assign_keyboard.
In your current code you call it in macro handler (every time some key is pressed) which is not necessary.
Petr Medek
LUAmacros author

Post Reply