detecting the SHIFT key on the Mac

  • Thread starter Laurent (de la cabane au Canada)
  • Start date
L

Laurent (de la cabane au Canada)

Hi,

I created a small macro on my pc (windows XP, Office XP), and it is able
to adjust its behavior according to the SHIFT key state. It works fine.
Unfortunately, it doesn't work on my friend's MacIntosh. I don't own a
Mac, so maybe someone out here can help me: how do I detect the SHIFT
key state on a Mac? And as a bonus question: how do I detect which OS
(Windows or MacOS) my workdook is opened from, so that I can call the
appropriate SHIFT detection routine accordingly?

Thanks!

Laurent
 
B

Bob Greenblatt

Hi,

I created a small macro on my pc (windows XP, Office XP), and it is able
to adjust its behavior according to the SHIFT key state. It works fine.
Unfortunately, it doesn't work on my friend's MacIntosh. I don't own a
Mac, so maybe someone out here can help me: how do I detect the SHIFT
key state on a Mac? And as a bonus question: how do I detect which OS
(Windows or MacOS) my workdook is opened from, so that I can call the
appropriate SHIFT detection routine accordingly?

Thanks!

Laurent

Why not just route the macro depending on the key that is pressed? For
example: application.onkey "a", "littleaprocedure"
Application.onkey "A","bigaprocedure"

You can determine the platform by application.operatingsystem
I use: bMac=left(application.operatingsystem,3)="Mac"
 
L

Laurent (de la cabane au Canada)

Hello Bob,
Why not just route the macro depending on the key that is pressed? For
example: application.onkey "a", "littleaprocedure"
Application.onkey "A","bigaprocedure"

Mmm...this is not exactly what I need, sorry I wasn't very specific. I
need to detect the Shift key being pressed in conjunction with some
Sheet buttons. In the pc version, that means declaring some external
(API) function. I have no idea how to do that on the Mac, but my guess
is there are some API functions on the Mac too for that purpose. Any
other idea on that?
You can determine the platform by application.operatingsystem
I use: bMac=left(application.operatingsystem,3)="Mac"

Good! I'll use that.

Regards,

Laurent
 
B

Bob Greenblatt

Hello Bob,


Mmm...this is not exactly what I need, sorry I wasn't very specific. I
need to detect the Shift key being pressed in conjunction with some
Sheet buttons. In the pc version, that means declaring some external
(API) function. I have no idea how to do that on the Mac, but my guess
is there are some API functions on the Mac too for that purpose. Any
other idea on that?


Good! I'll use that.

Regards,

Laurent
If the buttons are on a user form, you ought to be able to use the keydown
event.
 
L

Laurent (de la cabane au Canada)

Bob said:
If the buttons are on a user form, you ought to be able to use the keydown event.

no userform, just sheet buttons... :-(((

Gee, I wish Mac had the same OS as PCs...

(really REALLY kidding here...pfff)

;-)))

Laurent
 
B

Bob Greenblatt

no userform, just sheet buttons... :-(((

Gee, I wish Mac had the same OS as PCs...

(really REALLY kidding here...pfff)

;-)))

Laurent
How are you detecting it on the PC?
 
L

Laurent (de la cabane au Canada)

Hi Bob,

Here is the code required to detect the SHIFT key state on a PC:

Private Declare Function GetKeyState Lib "User32" (ByVal vKey As Long)
As Integer

Sub DetectShiftKey()
If GetKeyState(&H10) < 0 Then MsgBox "SHIFT key is depressed"
End Sub



Hope this help. Thanks!

Laurent
 
B

Bob Greenblatt

Hi Bob,

Here is the code required to detect the SHIFT key state on a PC:

Private Declare Function GetKeyState Lib "User32" (ByVal vKey As Long)
As Integer

Sub DetectShiftKey()
If GetKeyState(&H10) < 0 Then MsgBox "SHIFT key is depressed"
End Sub



Hope this help. Thanks!

Laurent
Other than the methods I described, I can't think of a way to do this on the
Mac. As far as I know there are no readily available calls into the
operating system that will give you this.
 
L

Laurent (de la cabane au Canada)

Bob said:
Other than the methods I described, I can't think of a way to do this on the
Mac. As far as I know there are no readily available calls into the
operating system that will give you this.

Hello Bob,

Maybe I'll take a chance on a programming forum for the Mac.

I know for sure there is a shift key on the Mac! So there MUST be some
way of getting its state!

I appreciate your help.

Regards,

Laurent
 
B

Bob Greenblatt

Hello Bob,

Maybe I'll take a chance on a programming forum for the Mac.

I know for sure there is a shift key on the Mac! So there MUST be some
way of getting its state!

I appreciate your help.

Regards,

Laurent
There is indeed a shift key. However, I wouldn't bet that it's state is
available to Excel or any other app. If you find a way, please let me know.
 
B

Bernard Rey

There is indeed a shift key. However, I wouldn't bet that it's state is
available to Excel or any other app. If you find a way, please let me know.

There *was* a way to have it through an AppleScript using "Jon's Commands"
OSAX. But I couldn't find an equivalent running in OS X (I'm still after it)
I'll let you know if I can get the hand on a solution...
 
L

Laurent (de la cabane au Canada)

Bob Greenblatt wrote:

There is indeed a shift key. However, I wouldn't bet that it's state is
available to Excel or any other app. If you find a way, please let me know.

I'll do so Bob, but so far I haven't found anything. I can't even find a
decent programming forum for the Mac! Maybe Macs are so sophisticated
that they don't need programming at all! Do Mac programmers exist at
all? What language do they program with?

Laurent
 
L

Laurent (de la cabane au Canada)

Bernard said:
There *was* a way to have it through an AppleScript using "Jon's Commands"
OSAX. But I couldn't find an equivalent running in OS X (I'm still after it)
I'll let you know if I can get the hand on a solution...

Hi Bernard,

Wow! Finally a interesting lead. I was about to quit my news program.
I'll keep watching then.

Laurent
 
B

Bernard Rey

Laurent said:
Wow! Finally a interesting lead. I was about to quit my news program.
I'll keep watching then.

OK, I got it! There's a new version! the "Jon's Commands X" 3.0d3 OSAX can
be downloaded from http://www.seanet.com/~jonpugh/

And this works (should work?) as expected:

Private Declare Function GetKeyState Lib "User32" (ByVal vKey As Long)
As Integer

Sub DetectShiftKey()

If Application.PathSeparator = ":" Then
MonScript = "tell application ""Finder""" & vbCr _
& "if (keys pressed) contains {""Shift""} then" & vbCr _
& "set result to ""Touche MAJ activée.""" _
& vbCr & "end if" & vbCr & "end tell"
temp = MacScript(MonScript)
MsgBox "Sur Mac " & temp

Else
If GetKeyState(&H10) < 0 Then MsgBox "Touche MAJ activée."
End If
End Sub

I've had it running with Excel 2004 and Excel 2003/Virtual PC

The only "problem" about it is that it requires the addition to be
installed, so it's not too handy if your workbook has to be shared.
 
P

Paul Berkowitz

OK, I got it! There's a new version! the "Jon's Commands X" 3.0d3 OSAX can
be downloaded from http://www.seanet.com/~jonpugh/

And this works (should work?) as expected:

Private Declare Function GetKeyState Lib "User32" (ByVal vKey As Long)
As Integer

Sub DetectShiftKey()

If Application.PathSeparator = ":" Then
MonScript = "tell application ""Finder""" & vbCr _
& "if (keys pressed) contains {""Shift""} then" & vbCr _
& "set result to ""Touche MAJ activée.""" _
& vbCr & "end if" & vbCr & "end tell"
temp = MacScript(MonScript)
MsgBox "Sur Mac " & temp

Else
If GetKeyState(&H10) < 0 Then MsgBox "Touche MAJ activée."
End If
End Sub

I've had it running with Excel 2004 and Excel 2003/Virtual PC

There's no need whatsoever to target the Finder in the MacScript
(AppleScript).

Sub DetectShiftKey()

Dim MonScript As String
Dim temp As Variant
Dim GetKeyState As Variant

If System.OperatingSystem = "Macintosh" Then
MonScript = ("if (keys pressed) contains {""Shift""} then" & vbCr & _
"set result to ""Shift Key depressed""" & vbCr & _
"else" & vbCr & _
"set result to ""Shift Key not depressed""" & vbCr & _
"end if")
temp = MacScript(MonScript)
MsgBox "On the Mac: " & vbCr & temp

Else
If GetKeyState(&H10) < 0 Then
MsgBox "On Windows:" & vbLf & "Shift Key depressed"
Else
MsgBox "On Windows:" & vbLf & "Shift Key not depressed"
End If
End If
End Sub


The only "problem" about it is that it requires the addition to be
installed, so it's not too handy if your workbook has to be shared.

You can easily bundle Jon's with an (AppleScript) installer

--
Paul Berkowitz
MVP MacOffice
Entourage FAQ Page: <http://www.entourage.mvps.org/faq/index.html>
AppleScripts for Entourage: <http://macscripter.net/scriptbuilders/>

Please "Reply To Newsgroup" to reply to this message. Emails will be
ignored.

PLEASE always state which version of Microsoft Office you are using -
**2004**, X or 2001. It's often impossible to answer your questions
otherwise.
 
B

Bernard Rey

Thank you, Paul.

Good to know there's no need for the "tell... end tell" statement targeting
the Finder! Does this mean the ³MacScript² instruction can be considered as
someting equivalent to a ³tell ³Excel²... end tell² instruction?

On the other hand, there¹s a problem in your version, the:
If System.OperatingSystem = "Macintosh" Then

....line won't run. It has to be changed to:
If Application.OperatingSystem Like "Mac*" Then

....or:
If Left(Application.OperatingSystem, 3) = "Mac" Then

....as the "OperationSystem" Property returned in my case is "Macintosh
(Power PC) 10.37".



Paul said:
There's no need whatsoever to target the Finder in the MacScript
(AppleScript).

Sub DetectShiftKey()

Dim MonScript As String
Dim temp As Variant
Dim GetKeyState As Variant

If System.OperatingSystem = "Macintosh" Then
MonScript = ("if (keys pressed) contains {""Shift""} then" & vbCr & _
"set result to ""Shift Key depressed""" & vbCr & _
"else" & vbCr & _
"set result to ""Shift Key not depressed""" & vbCr & _
"end if")
temp = MacScript(MonScript)
MsgBox "On the Mac: " & vbCr & temp

Else
If GetKeyState(&H10) < 0 Then
MsgBox "On Windows:" & vbLf & "Shift Key depressed"
Else
MsgBox "On Windows:" & vbLf & "Shift Key not depressed"
End If
End If
End Sub


You can easily bundle Jon's with an (AppleScript) installer
 
P

Paul Berkowitz

Thank you, Paul.

Good to know there's no need for the "tell... end tell" statement targeting
the Finder! Does this mean the ³MacScript² instruction can be considered as
someting equivalent to a ³tell ³Excel²... end tell² instruction?
No. Scripting Addition commands do not necessarily (and do not usually) have
to be directed at an application. You're not scripting the Finder here, nor
are you scripting the 'current application' (which I think is what you're
asking re Excel). In some cases - for example 'display dialog' - one of the
Standard Additions - it is in fact directed to the current application if
not in a tell block. But most other osax commands including 'keys pressed'
have nothing to do with any application any more than

set a to 24

is directed to an application. This one is just pure AppleScript
(AppleScript is a system component), and scripting additions are just that -
additions or extensions to AppleScript.
On the other hand, there¹s a problem in your version, the:
If System.OperatingSystem = "Macintosh" Then

...line won't run. It has to be changed to:
If Application.OperatingSystem Like "Mac*" Then

...or:
If Left(Application.OperatingSystem, 3) = "Mac" Then

...as the "OperationSystem" Property returned in my case is " ;Macintosh
(Power PC) 10.37".

How strange. What's the issue with your system version? (And what's that "
;" at the beginning? Left(,3) isn't going to work there.)
System.OperatingSystem = "Macintosh" on my machine - I tested it. I'm
running 10.3.8.

Also, you twice suggest Application.OperatingSystem. Are you sure?
OperatingSystem is not a Property of Application, it's a Property of System
object (or of Application.System if you insist). Was that just a slip, or
does it really work for you? The Application.OperatingSystem syntax errors
here.

--
Paul Berkowitz
MVP MacOffice
Entourage FAQ Page: <http://www.entourage.mvps.org/faq/index.html>
AppleScripts for Entourage: <http://macscripter.net/scriptbuilders/>

Please "Reply To Newsgroup" to reply to this message. Emails will be
ignored.

PLEASE always state which version of Microsoft Office you are using -
**2004**, X or 2001. It's often impossible to answer your questions
otherwise.
 
B

Bernard Rey

Paul Berkowitz :
No. Scripting Addition commands do not necessarily (and do not usually) have
to be directed at an application. You're not scripting the Finder here, nor
are you scripting the 'current application' (which I think is what you're
asking re Excel). In some cases - for example 'display dialog' - one of the
Standard Additions - it is in fact directed to the current application if
not in a tell block. But most other osax commands including 'keys pressed'
have nothing to do with any application any more than

set a to 24

is directed to an application. This one is just pure AppleScript
(AppleScript is a system component), and scripting additions are just that -
additions or extensions to AppleScript.

Ok - I'll try and remember :)
How strange. What's the issue with your system version? (And what's that "
;" at the beginning? Left(,3) isn't going to work there.)

Strange indeed! I didn't have that semi column and didn't type it. I can't
even see it on my previous post. So it's (obviously) some artefact.
System.OperatingSystem = "Macintosh" on my machine - I tested it. I'm
running 10.3.8.

Also, you twice suggest Application.OperatingSystem. Are you sure?
OperatingSystem is not a Property of Application, it's a Property of System
object (or of Application.System if you insist). Was that just a slip, or
does it really work for you? The Application.OperatingSystem syntax errors
here.

I know what's happening here: you've been testing with MS Word, where
OperationSystem *is* a Property of System. But this is not so in Excel (where
there is no System class at all. In an Excel macro, System.OperatingSystem
results in an immediate syntax error.

I think this is one reason why I usually rely on the
"Application.PathSeparator" to test the current system. Another being that it
if often useful to have it in order to write path changes in the Macro.
 
B

Bob Greenblatt

Paul,

I don¹t know what the VB statement ³system² is. It¹s not in the object
browser. Perhaps it¹s an apple script object, not VB. In any case, what
Bernard was trying to explain is in VB, it¹s Application.operatingsystem.
This returns a string like²Macintosh (PowerPC) 10.38². So to determine the
platform look at the left 3 characters.

Now, for the dumb question: How does the jon¹s commands.osax get installed?
Where do I put it?
 
B

Bernard Rey

Bob Greenblatt:
Now, for the dumb question: How does the jon's commands.osax get installed?
Where do I put it?

Place it in the "/Library/ScriptingAdditions/" Folder.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top