Livecode - How to use keyDown? - livecode

I've been searching for a long time now and I wanted to know,
How can I code that things will happens when I press on a specific key, (e.g. W).
I found many threads that help in this section with general keys like the delete key or the spacebar and some keyDown theKey, but how do I define "theKey"?
Please help,
FESFEW

For the alphanumeric keys you can use as follows:
on keyDown thekey
switch thekey
case "w"
answer "w key pressed"
break
default
pass keyDown
end switch
end keyDown
KeyDown is an event that will be generated each time the user presses a keyboard symbol (not the function keys), the thekey parameter will contain the symbol of the pressed key.
On the other hand, if you want to differentiate capital letters you must set the CaseSensitive property to TRUE
set the caseSensitive to true
The rawKeyDown property works similarly, but instead of coming in the parameter, the symbol of the pressed key what you will get will be the code of the pressed key including the function keys like shift, F# etc.
on rawKeyDown theKeyCode
switch theKeyCode
case 32
answer "SPACE KEY"
break
case 119
answer "w key pressed"
break
default
pass rawKeyDown
end switch
end rawKeyDown
Constants will be very useful to help make your code more readable, adding constants to previous code will look like this (note that the uppercase keys have codes other than the lowercase ):
constant kSpaceKey = 32
constant kwKey = 119
constant kWUpperKey = 87
on rawKeyDown theKeyCode
switch theKeyCode
case kSpaceKey
answer "SPACE KEY"
break
case kwKey
answer "w key pressed"
break
case kwKey
answer "w key pressed"
break
case kWUpperKey
answer "upper W key pressed"
break
default
pass rawKeyDown
end switch
end rawKeyDown
You can also use this script in the card script to show the code of the key you press:
on rawKeyDown theKeyCode
put theKeyCode
end rawKeyDown
Always remember to pass rawKeyDown, rawKeyUp,KeyDown and KeyUp events if you
want to allow them to continue normal message flow

Related

AHK in MS Excel (Cell Selection Issue With Script)

I started a little script using CAPSLock key for navigation (This is only part of the code):
SetCapsLockState, AlwaysOff
CapsLock & i::
if getkeystate("alt") = 0
Send,{Up}
else
Send,+{Up}
return
CapsLock & l::
if getkeystate("alt") = 0
Send,{Right}
else
send, +{Right}
return
It works perfectly everywhere.. except in MS Excel!
When I select a range of cells the selection marquee is there but no cell reference is taken in the formula.
Ex: Summing C3:C10 turns into =sum() no cells are actually selected in the formula.
if I keep trying up and down many times.. it shows, but never consistent.
Any idea how to fix this?
Thank you in advance
The problem comes from your shift key being released between the key presses.
Like for example try pressing keys like this in Excel and you'll see you're not actually selecting cells, you're just highlighting them:
Shift Down, Up Arrow, Shift Up
Shift Down, Up Arrow, Shift Up
Shift Down, Up Arrow, Shift Up
...
So you're going to have to keep shift pressed for the whole duration of your selection making process.
Here's what I could come up with:
CapsLock::Shift
#If, GetKeyState("CapsLock", "P")
*i::
if (GetKeyState("Alt", "P"))
SendInput, {Blind}{Alt Up}{Up}
else
SendInput, {Up}
return
*l::
if (GetKeyState("Alt", "P"))
SendInput, {Blind}{Alt Up}{Right}
else
SendInput, {Down}
return
#If
Or just like this to write it a lot cleaner with a ternary:
CapsLock::Shift
#If, GetKeyState("CapsLock", "P")
*i::SendInput, % GetKeyState("Alt", "P") ? "{Blind}{Alt Up}{Up}" : "{Up}"
*l::SendInput, % GetKeyState("Alt", "P") ? "{Blind}{Alt Up}{Right}" : "{Right}"
#If
So what's happening?
First we use the simple remapping syntax(docs) to remap CapsLock to Shift.
Then we create context sensitive hotkeys for I and L with #If(docs).
The hotkeys are only triggered if the physical state of CapsLock is down.
We need the physical state, because the logical state (which is default) was remapped to shift.
The hotkeys are created with the * modifier(docs) so they fire even if Shift and/or Alt is held down.
I switched the Send commands SendInput due to it being the recommended faster and more reliable send mode(docs).
And then about {Blind}. Normally, when you just send something with a send command, it automatically releases any modifiers you may be holding. But if you use the blind send mode(docs), this doesn't happen. So Shift is still held down all the time.

How to assign values in popup menu item instead of choice 1 choice 2 in livecode

How to change this choice 1, choice 2 ,choice 3 in popu menu to special words (passing by variables from array). With out using the popup properties menu. Means: contents of array svar[2] instead of choice 1
contents of array svar[3] instead of choice 2 .. so on.
so each time the value of choice 1, choice 2 will differ.
global searchStr global replaceStr global Ftext global myArrayToBe global myArraylength global gvar on menuPick pItemName put the number of lines of (the keys of myArrayToBe) into myArraylength
repeat with i=1 to myArraylength if myArrayToBe[i] contains Ftext then put myArrayToBe[i] into Svar answer Svar split Svar by colon put Svar[2] into gvar answer gvar end if end repeat switch pItemName put gvar into pitemName case gvar answer Ftext break case "Choice 2" answer "bye" break case "Choice 3"answer "Please" break end switch end menuPick
Hard to see in your question what you are asking for, but you can set the menu options by using the text of button
If you want to change the menu on the fly when the user clicks, you can do that in the on mouseDown handler:
on mouseDown
set the text of me to "One" & return & "Two" & return & "three"
end mouseDown
if you then have a global variable sVar and would like to populate the menu just when it is about to be shown you can do that also:
on mouseDown
global sVar
put sVar into tVar # Copy array
combine tVar with return
set the text of me to tVar
end mouseDown
If you want to change the first two alternatives based on an array sVaryou can use:
put the text of button "myMenuButton" into tText
put sVar[1] into line 1 of tText
put sVar[2] into line 2 of tText
set the text of button "myMenuButton" to tText

How to add brackets using AutoHotKey

I want to create a hotkey Ctrl + ( that adds brackets to a phrase. I.e select x-1 to get (x-1). How to program this function?
I write a lot of phrases such as: x+1/(x-1)^2 so it would be helpful to have a hotkey to add brackets.
^(::
SendInput, ^c
Sleep 10
Clipboard = (%Clipboard%)
SendInput, ^v
return
This implies that you are actually pressing CTRL+SHIFT+9 (since you don't have a ( key).
I did a quick test and it will add round brackets to anything you highlight. I would recommend tweaking the trigger key since CTRL+SHIFT+9 isn't that easy to hit, but otherwise seems to work without issues.
If you want to save the clipboard, then you'll have to do this:
^(::
SavedClipboard := ClipboardAll
SendInput, ^c
Sleep 10
Clipboard = (%Clipboard%)
SendInput, ^v
Clipboard := SavedClipboard
SavedClipboard =
return

Delete a character from string realbasic

I have a virtual keyboard created in RealBasic.
When I press letters, numbers I append it to a textfield all its ok but, how do I delete characters from that textfield from current cursor position when I press "Delete" button from virtual keyboard?
To append letters or numbers to the textfields I use:
TextField1.Text = TextField1.text + me.Caption //to append caption
TextField1.SelStart = Len(TextField1.text) // to move cursor at the end of string
Paul's solution works if you only plan to delete the last typed character.
But beware: If you let the user also move the cursor left and right, you have to delete the text at the position of the cursor, of course. And if you also allow the user to select text, then it's even more complicated.
I suggest that your virtual keyboard simply send the typed key to the system as if the user had pressed the key. That way, the TextEdit field will do everything for you.
To make this work, however, you need custom solutions for each OS platform you want to support.
Let me know which platforms you plan to support and I'll see what I can find. I have some code for OSX but not for Windows, yet.
Doing what Thomas said means:
dim n as String = TextField1.Text
n = newText.left(TextField1.selStart) + n.right(n.len - textField1.selStart - 1)
textField1.text = n
Just lop off the last character:
TextField1.Text = TextField1.Text.Left(TextField1.Len-1)

Autohotkey remapping alt+j to left but alt+shift+j doesn't select left nor do alt+ctr+j move a caret , if getKeyState does not work

Basically,
It's inspired by Vim I want to use a key(e.g Alt, F1) combination(+I J K L) to map to Arrow Keys
What is already done in Autohotkey
Ralt & j::send{Left}
Ralt & k::send{Right}
...
Now I take Alt+I as up etc,which is pretty fine for me But the problem comes when you press
Ralt+Shift+j (Suppose to select the last charater)
Ralt+Ctrl+j (Suppose to move a caramel text)
These kind of combination would not work and it just get overrided to basic move cursor to left
Even if I use if/while statement with GetKeyState, it doesn't gonna work
if GetKeyState("Shift","P")
Ralt+j::send +{Left}
This kind of stuff didn't work
Any Ideas on that ?It would make coding very efficient without having to move the right hand.
Thanks in advance.
You are missing 2 things:
Must use a # symbol when doing a context sensitive hotkey
The bottom section of code is using a + instead of the & you used previously.
See the below modification:
RAlt & j:: Send {Left}
RAlt & k:: Send {Right}
#If GetKeyState("Shift","P")
RAlt & j:: Send +{Left}
RAlt & k:: Send +{Right}
; Close with #If to remove the context or simply start another '#If GetKeystate ....'

Resources