Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default use of arrow keys in Excel

Hey, I am woundering for a simple game that I am making, wheather it
will be possible to use the arrow keys to move an object, right now I
have been using a command button, and using the acceleratior option,
but it is annoying to put alt + another key,
if you could tell me this I will be truly greatful
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default use of arrow keys in Excel

You could OnKeys to run a macro to move the object when the arrow keys are
presed.
--
Gary''s Student - gsnu2007k


"Dean" wrote:

Hey, I am woundering for a simple game that I am making, wheather it
will be possible to use the arrow keys to move an object, right now I
have been using a command button, and using the acceleratior option,
but it is annoying to put alt + another key,
if you could tell me this I will be truly greatful

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default use of arrow keys in Excel

What object are you trying to move? Where is the object at (the worksheet or
a UserForm)?

--
Rick (MVP - Excel)


"Dean" wrote in message
...
Hey, I am woundering for a simple game that I am making, wheather it
will be possible to use the arrow keys to move an object, right now I
have been using a command button, and using the acceleratior option,
but it is annoying to put alt + another key,
if you could tell me this I will be truly greatful


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default use of arrow keys in Excel

On Jan 11, 7:44*pm, "Rick Rothstein"
wrote:
What object are you trying to move? Where is the object at (the worksheet or
a UserForm)?

--
Rick (MVP - Excel)

"Dean" wrote in message

...



Hey, I am woundering for a simple game that I am making, wheather it
will be possible to use the arrow keys to move an object, right now I
have been using a command button, and using the acceleratior option,
but it is annoying to put alt + another key,
if you could tell me this I will be truly greatful- Hide quoted text -


- Show quoted text -


I'm using a userform, control toolbox and I just want to move a
picture left and right
at the moment I am using this code

Dim i As Integer

Private Sub Cmdright_Click()
i = i + 1
Image1.Left = i
End Sub

thanks
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default use of arrow keys in Excel

I haven't looked into it in great detail, but I think moving an Image
control with the arrow keys may prove to be somewhat hard to do. However, I
do have a routine that will allow you to move the Image control around with
your mouse. Here is that code for that if you can make use of it. Copy/Paste
the code below into the UserForm's code window (if you have any existing
MouseDown or MouseMove event code, you will have to integrate it into the
event procedures below). As coded, you need to press the Shift key when you
left click the mouse on the Image control (and keep them depressed) while
you drag the picture around. If you want to use the left mouse button all by
itself, then change this line in the MouseMove event....

If Button = 1 And Shift = 1 Then

to this...

If Button = 1 Then

Note... I used the Shift key coupled with the left mouse button for its
definitional description (Shift meaning *shift* the picture).

'*************** START OF CODE ***************
Private Type POINTAPI
X As Long
Y As Long
End Type

Private Declare Function GetCursorPos Lib "user32" _
(lpPoint As POINTAPI) As Long

Private Declare Function GetDC Lib "user32" _
(ByVal hwnd As Long) As Long

Private Declare Function GetDeviceCaps Lib "gdi32" _
(ByVal hDC As Long, _
ByVal nIndex As Long) As Long

Private Declare Function ReleaseDC Lib "user32" _
(ByVal hwnd As Long, _
ByVal hDC As Long) As Long

Private Const LOGPIXELSX = 88 'Pixels/inch in X

'A point is defined as 1/72 inches
Private Const POINTS_PER_INCH As Long = 72

Dim Initialized As Boolean

'The size of a pixel, in points
Private Function PointsPerPixel() As Double
Dim hDC As Long
Dim lDotsPerInch As Long
hDC = GetDC(0)
lDotsPerInch = GetDeviceCaps(hDC, LOGPIXELSX)
PointsPerPixel = POINTS_PER_INCH / lDotsPerInch
ReleaseDC 0, hDC
End Function

Private Sub Image1_MouseMove(ByVal Button As Integer, _
ByVal Shift As Integer, _
ByVal X As Single, _
ByVal Y As Single)
Static LastPoint As POINTAPI
Dim CurrentPoint As POINTAPI
If Button = 1 And Shift = 1 Then
GetCursorPos CurrentPoint
If Not Initialized Then
LastPoint.X = CurrentPoint.X
LastPoint.Y = CurrentPoint.Y
Initialized = True
End If
With Image1
.Move .Left + PointsPerPixel * (CurrentPoint.X - LastPoint.X), _
.Top + PointsPerPixel * (CurrentPoint.Y - LastPoint.Y)
End With
LastPoint = CurrentPoint
End If
End Sub

Private Sub Image1_MouseUp(ByVal Button As Integer, _
ByVal Shift As Integer, _
ByVal X As Single, _
ByVal Y As Single)
Initialized = False
End Sub
'*************** END OF CODE ***************

--
Rick (MVP - Excel)


"Dean" wrote in message
...
On Jan 11, 7:44 pm, "Rick Rothstein"
wrote:
What object are you trying to move? Where is the object at (the worksheet
or
a UserForm)?

--
Rick (MVP - Excel)

"Dean" wrote in message

...



Hey, I am woundering for a simple game that I am making, wheather it
will be possible to use the arrow keys to move an object, right now I
have been using a command button, and using the acceleratior option,
but it is annoying to put alt + another key,
if you could tell me this I will be truly greatful- Hide quoted text -


- Show quoted text -


I'm using a userform, control toolbox and I just want to move a
picture left and right
at the moment I am using this code

Dim i As Integer

Private Sub Cmdright_Click()
i = i + 1
Image1.Left = i
End Sub

thanks



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default use of arrow keys in Excel

Hey thanks alot, even though that wasn't quite what I was looking for,
I may use it on later projects,
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default use of arrow keys in Excel

On Jan 12, 1:17*pm, Dean wrote:
Hey thanks alot, even though that wasn't quite what I was looking for,
I may use it on later projects,


hello, yea I found it out from an old example I got from the Internet,
the code it
Private Sub CommandButton1_KeyDown(ByVal KeyCode As
MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 1 Then
MsgBox hello world
End If

End Sub

but the only problem is I don't know what key cone is which, I think
the left arrow is 37, but how could I find these no's out???
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default use of arrow keys in Excel

But that code is for a CommandButton... you said you wanted to move an Image
control around. The reason I said I think it may be hard to do that is
because the Image control cannot take focus and it does not have a KeyDown
(nor a KeyUp or KeyPress) event. You might be able to put code in every
other control's KeyDown event procedure if you don't have too many other
controls on the UserForm and handle the problem that way. If you want to do
it that way, copy/paste this subroutine into the UserForm's code window...

Sub MoveImage1(ByVal Direction As Long)
Const MoveAmount As Long = 2
Select Case Direction
Case vbKeyLeft
Image1.Left = Image1.Left - MoveAmount
Case vbKeyRight
Image1.Left = Image1.Left + MoveAmount
Case vbKeyUp
Image1.Top = Image1.Top - MoveAmount
Case vbKeyDown
Image1.Top = Image1.Top + MoveAmount
End Select
End Sub

and, for **every** control on the UserForm that has a KeyDown event
procedure available, put this single line in that KeyDown event...

MoveImage1 KeyCode

--
Rick (MVP - Excel)


"Dean" wrote in message
...
On Jan 12, 1:17 pm, Dean wrote:
Hey thanks alot, even though that wasn't quite what I was looking for,
I may use it on later projects,


hello, yea I found it out from an old example I got from the Internet,
the code it
Private Sub CommandButton1_KeyDown(ByVal KeyCode As
MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 1 Then
MsgBox hello world
End If

End Sub

but the only problem is I don't know what key cone is which, I think
the left arrow is 37, but how could I find these no's out???

  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default use of arrow keys in Excel

By the way, I forgot to mention that you can control how far the Image moves
using the MoveAmount constant declared in the subroutine's Const
statement... experiment with some values to see which movement amount you
like.

--
Rick (MVP - Excel)


"Rick Rothstein" wrote in message
...
But that code is for a CommandButton... you said you wanted to move an
Image control around. The reason I said I think it may be hard to do that
is because the Image control cannot take focus and it does not have a
KeyDown (nor a KeyUp or KeyPress) event. You might be able to put code in
every other control's KeyDown event procedure if you don't have too many
other controls on the UserForm and handle the problem that way. If you
want to do it that way, copy/paste this subroutine into the UserForm's
code window...

Sub MoveImage1(ByVal Direction As Long)
Const MoveAmount As Long = 2
Select Case Direction
Case vbKeyLeft
Image1.Left = Image1.Left - MoveAmount
Case vbKeyRight
Image1.Left = Image1.Left + MoveAmount
Case vbKeyUp
Image1.Top = Image1.Top - MoveAmount
Case vbKeyDown
Image1.Top = Image1.Top + MoveAmount
End Select
End Sub

and, for **every** control on the UserForm that has a KeyDown event
procedure available, put this single line in that KeyDown event...

MoveImage1 KeyCode

--
Rick (MVP - Excel)


"Dean" wrote in message
...
On Jan 12, 1:17 pm, Dean wrote:
Hey thanks alot, even though that wasn't quite what I was looking for,
I may use it on later projects,


hello, yea I found it out from an old example I got from the Internet,
the code it
Private Sub CommandButton1_KeyDown(ByVal KeyCode As
MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 1 Then
MsgBox hello world
End If

End Sub

but the only problem is I don't know what key cone is which, I think
the left arrow is 37, but how could I find these no's out???


  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default use of arrow keys in Excel

It just occurred to me that, using the code I provided, it would be possible
to move the image out of view (off, way off, the edges of the UserForm).
Here is a modified subroutine that will stop the movement when the Image
control reaches the edges of the UserForm...

Sub MoveImage1(ByVal Direction As Long)
Const MoveAmount As Long = 2
With Image1
Select Case Direction
Case vbKeyLeft
If .Left MoveAmount Then .Left = .Left - MoveAmount
Case vbKeyRight
If .Left < Me.InsideWidth - .Width - MoveAmount _
Then .Left = .Left + MoveAmount
Case vbKeyUp
If .Top MoveAmount Then .Top = .Top - MoveAmount
Case vbKeyDown
If .Top < Me.InsideHeight - .Height - MoveAmount _
Then .Top = .Top + MoveAmount
End Select
End With
End Sub

--
Rick (MVP - Excel)


"Rick Rothstein" wrote in message
...
But that code is for a CommandButton... you said you wanted to move an
Image control around. The reason I said I think it may be hard to do that
is because the Image control cannot take focus and it does not have a
KeyDown (nor a KeyUp or KeyPress) event. You might be able to put code in
every other control's KeyDown event procedure if you don't have too many
other controls on the UserForm and handle the problem that way. If you
want to do it that way, copy/paste this subroutine into the UserForm's
code window...

Sub MoveImage1(ByVal Direction As Long)
Const MoveAmount As Long = 2
Select Case Direction
Case vbKeyLeft
Image1.Left = Image1.Left - MoveAmount
Case vbKeyRight
Image1.Left = Image1.Left + MoveAmount
Case vbKeyUp
Image1.Top = Image1.Top - MoveAmount
Case vbKeyDown
Image1.Top = Image1.Top + MoveAmount
End Select
End Sub

and, for **every** control on the UserForm that has a KeyDown event
procedure available, put this single line in that KeyDown event...

MoveImage1 KeyCode

--
Rick (MVP - Excel)


"Dean" wrote in message
...
On Jan 12, 1:17 pm, Dean wrote:
Hey thanks alot, even though that wasn't quite what I was looking for,
I may use it on later projects,


hello, yea I found it out from an old example I got from the Internet,
the code it
Private Sub CommandButton1_KeyDown(ByVal KeyCode As
MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 1 Then
MsgBox hello world
End If

End Sub

but the only problem is I don't know what key cone is which, I think
the left arrow is 37, but how could I find these no's out???




  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default use of arrow keys in Excel

On Jan 12, 8:28*pm, "Rick Rothstein"
wrote:
It just occurred to me that, using the code I provided, it would be possible
to move the image out of view (off, way off, the edges of the UserForm).
Here is a modified subroutine that will stop the movement when the Image
control reaches the edges of the UserForm...

Sub MoveImage1(ByVal Direction As Long)
* Const MoveAmount As Long = 2
* With Image1
* * Select Case Direction
* * * Case vbKeyLeft
* * * * If .Left MoveAmount Then .Left = .Left - MoveAmount
* * * Case vbKeyRight
* * * * If .Left < Me.InsideWidth - .Width - MoveAmount _
* * * * * * * * * * * * Then .Left = .Left + MoveAmount
* * * Case vbKeyUp
* * * * If .Top MoveAmount Then .Top = .Top - MoveAmount
* * * Case vbKeyDown
* * * * If .Top < Me.InsideHeight - .Height - MoveAmount _
* * * * * * * * * * * * * *Then .Top = .Top + MoveAmount
* * End Select
* End With
End Sub

--
Rick (MVP - Excel)

"Rick Rothstein" wrote in message

...



But that code is for a CommandButton... you said you wanted to move an
Image control around. The reason I said I think it may be hard to do that
is because the Image control cannot take focus and it does not have a
KeyDown (nor a KeyUp or KeyPress) event. You might be able to put code in
every other control's KeyDown event procedure if you don't have too many
other controls on the UserForm and handle the problem that way. If you
want to do it that way, copy/paste this subroutine into the UserForm's
code window...


Sub MoveImage1(ByVal Direction As Long)
*Const MoveAmount As Long = 2
*Select Case Direction
* *Case vbKeyLeft
* * *Image1.Left = Image1.Left - MoveAmount
* *Case vbKeyRight
* * *Image1.Left = Image1.Left + MoveAmount
* *Case vbKeyUp
* * *Image1.Top = Image1.Top - MoveAmount
* *Case vbKeyDown
* * *Image1.Top = Image1.Top + MoveAmount
*End Select
End Sub


and, for **every** control on the UserForm that has a KeyDown event
procedure available, put this single line in that KeyDown event...


MoveImage1 KeyCode


--
Rick (MVP - Excel)


"Dean" wrote in message
....
On Jan 12, 1:17 pm, Dean wrote:
Hey thanks alot, even though that wasn't quite what I was looking for,
I may use it on later projects,


hello, yea I found it out from an old example I got from the Internet,
the code it
Private Sub CommandButton1_KeyDown(ByVal KeyCode As
MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 1 Then
MsgBox hello world
End If


End Sub


but the only problem is I don't know what key cone is which, I think
the left arrow is 37, but how could I find these no's out???- Hide quoted text -


- Show quoted text -


Yea thanks for your reply, I knew you could use the call function for
Commandbutton_keypress, but didn't mention it,
I found out the key code's from a MSDN form, but can't rember the
link,
Sorry for complicating thing with using the command button, and not
using the command button,
Thanks for all you replies
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Arrow keys in Excel mitch Excel Discussion (Misc queries) 4 October 5th 09 04:27 AM
<!Excel and Arrow Keys! Torrin Miller Excel Worksheet Functions 1 January 4th 07 11:28 PM
Why don't my arrow keys work in Excel? beckybun Excel Discussion (Misc queries) 3 July 25th 06 09:20 PM
Arrow Keys in Excel Claude Excel Discussion (Misc queries) 3 August 5th 05 04:20 AM
arrow keys in excel agouldma Excel Discussion (Misc queries) 1 January 26th 05 07:29 PM


All times are GMT +1. The time now is 10:05 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"