2016-11-23 96 views
2

我有一個vb 6程序,其中窗體打開像一個電源點幻燈片我創建了一個文本框獲取鍵盤輸入像esc,左鍵,右鍵,向上鍵或向下鍵就像MS幻燈片。但是,當向表單添加許多控件時,用戶可能會點擊其中的任何一個,並且附加到我的文本框控件(我稱爲cmdline)的鍵碼將不起作用,除非向其他控件添加另一個代碼以強制將光標焦點cmdline聆聽鍵盤輸入

Private Sub cmdline_KeyDown(KeyCode As Integer, Shift As Integer) 
     'Space Key 
     If KeyCode = 32 Then 
      cmdPlay_Click 
     End If 

     'Page Up Key 
     If KeyCode = 33 Then 
      showFirstStanza 
     End If 

     'Page Down Key 
     If KeyCode = 34 Then 
      showLastStanza 
     End If 

     'End Key 
     If KeyCode = 35 Then 
      showLastStanza 
     End If 

     'Home Key 
     If KeyCode = 36 Then 
      showFirstStanza 
     End If 

     'Left key 
     If KeyCode = 37 Then 
      SavedThis = SaveSettings("projection_font_size", SiteSettings("projection_font_size") - 2) 
      lblSongText.FontSize = SiteSettings("projection_font_size") 
     End If 

     'Up Key 
     If KeyCode = 38 Then 
      cmdPrev_Click 
     End If 

     'Right Arrow 
     If KeyCode = 39 Then 
      SavedThis = SaveSettings("projection_font_size", SiteSettings("projection_font_size") + 2) 
      lblSongText.FontSize = SiteSettings("projection_font_size") 
     End If 

     'Down Arrow 
     If KeyCode = 40 Then 
      cmdNext_Click 
     End If 

     'Key C 
     If KeyCode = 67 Then 
      fonttypez = fonttypez - 1 
      Select Case fonttypez 
      Case 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 
       font_type_tosave 
      Case Else 
       fonttypez = 13 
       font_type_tosave 
      End Select 
       font_type_int 
       lblSongText.FontName = SiteSettings("projection_font_type") 
     End If 

     'Key V 
     If KeyCode = 86 Then 
      fonttypez = fonttypez + 1 
      Select Case fonttypez 
      Case 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 
       font_type_tosave 
      Case Else 
       fonttypez = 13 
       font_type_tosave 
      End Select 
       font_type_int 
       lblSongText.FontName = SiteSettings("projection_font_type") 
     End If 

     'Key X 
     If KeyCode = 88 Then 
      kala = kala + 1 
      Select Case kala 
      Case 1, 2, 3, 4, 5, 6, 7, 8 
       SavedThis = SaveSettings("preffered_theme", kala) 
      Case Else 
       kala = 1 
       SavedThis = SaveSettings("preffered_theme", kala) 
      End Select 
       SetProjectionTheme 
     End If 

     'Key Z 
     If KeyCode = 90 Then 
      kala = kala - 1 
      Select Case kala 
      Case 1, 2, 3, 4, 5, 6, 7, 8 
       SavedThis = SaveSettings("preffered_theme", kala) 
      Case Else 
       kala = 1 
       SavedThis = SaveSettings("preffered_theme", kala) 
      End Select 
       SetProjectionTheme 
     End If 

     'Add Key 
     If KeyCode = 107 Then 
      SavedThis = SaveSettings("projection_font_size", SiteSettings("projection_font_size") + 2) 
      lblSongText.FontSize = SiteSettings("projection_font_size") 
     End If 

     'Subtract Key 
     If KeyCode = 109 Then 
      SavedThis = SaveSettings("projection_font_size", SiteSettings("projection_font_size") - 2) 
      lblSongText.FontSize = SiteSettings("projection_font_size") 
     End If 
    End Sub 

是否有人有一個代碼,可以幫助我沒有控制附加到它的鍵盤輸入。

順便說一句,使用菜單非常酷,因爲無論哪個控件被聚焦,鍵盤輸入都會很容易被捕獲。我不想使用這個表單上的菜單,除非它可以被隱藏。

+2

[KeyPreview屬性:MSDN Visual Basic Reference](https://msdn.microsoft.com/en-us/library/aa235137(v = vs.60).aspx) –

+0

但我沒有看到任何引用的內容 –

+0

另外,您可能需要考慮使用'Select Case KeyCode','Case 32','cmdPlay_Click','Case ...','End Select'。你編碼它的方式,*所有*'If'語句在*每個*鍵擊上執行。 – Herb

回答

2

要實現你想要的東西:

  • 有需要在頂部聲明爲你 在我的代碼中看到的功能。
  • 然後編寫一個函數,讓鍵盤上的控件按下。
  • 您將需要一個循環或類似計時器的東西與 1000使用的間隔來調用你的函數

下面是修改後的代碼實現你想要的

'declare this function at the top of your form:   
     Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer 
    Public Function getControls() 
     'Space Key 
     If GetAsyncKeyState(vbKeySpace) <> 0 Then 'code 32 
      cmdPlay_Click 
     End If  
     'Page Up Key 
     If GetAsyncKeyState(vbKeyPageUp) <> 0 Then 'code 33 
      showFirstStanza 
     End If 

     'Page Down Key 
     If GetAsyncKeyState(vbKeyPageDown) <> 0 Then 'code 34 
      showLastStanza 
     End If 

     'End Key 
     If GetAsyncKeyState(vbKeyEnd) <> 0 Then 'code 35 
      showLastStanza 
     End If 

     'Home Key 
     If GetAsyncKeyState(vbKeyHome) <> 0 Then 'code 36 
      showFirstStanza 
     End If 

     'Left key 
     If GetAsyncKeyState(vbKeyLeft) <> 0 Then 'code 37 
       SavedThis = SaveSettings("projection_font_size", SiteSettings("projection_font_size") - 2) 
       lblSongText.FontSize = SiteSettings("projection_font_size") 
     End If 

     'Up Key 
     If GetAsyncKeyState(vbKeyUp) <> 0 Then 'code 38 
      cmdPrev_Click 
     End If 

     'Right Arrow 
     If GetAsyncKeyState(vbKeyRight) <> 0 Then 'code 39 
       SavedThis = SaveSettings("projection_font_size", SiteSettings("projection_font_size") + 2) 
       lblSongText.FontSize = SiteSettings("projection_font_size") 
     End If 

     'Down Arrow 
     If GetAsyncKeyState(vbKeyDown) <> 0 Then 'code 40 
      cmdNext_Click 
     End If 

     'Key C 
     If GetAsyncKeyState(vbKeyC) <> 0 Then 'code 67 
      fonttypez = fonttypez - 1 
      Select Case fonttypez 
      Case 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 
       font_type_tosave 
      Case Else 
       fonttypez = 13 
       font_type_tosave 
      End Select 
       font_type_int 
       lblSongText.FontName = SiteSettings("projection_font_type") 
     End If 

     'Key V 
     If GetAsyncKeyState(vbKeyV) <> 0 Then 'code 87 
      fonttypez = fonttypez + 1 
      Select Case fonttypez 
      Case 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 
       font_type_tosave 
      Case Else 
       fonttypez = 13 
       font_type_tosave 
      End Select 
       font_type_int 
       lblSongText.FontName = SiteSettings("projection_font_type") 
     End If 

     'Key X 
     If GetAsyncKeyState(vbKeyX) <> 0 Then 'code 88 
      kala = kala + 1 
      Select Case kala 
      Case 1, 2, 3, 4, 5, 6, 7, 8 
       SavedThis = SaveSettings("preffered_theme", kala) 
      Case Else 
       kala = 1 
       SavedThis = SaveSettings("preffered_theme", kala) 
      End Select 
       SetProjectionTheme 
     End If 

     'Key Z 
     If GetAsyncKeyState(vbKeyZ) <> 0 Then 'code 90 
      kala = kala - 1 
      Select Case kala 
      Case 1, 2, 3, 4, 5, 6, 7, 8 
       SavedThis = SaveSettings("preffered_theme", kala) 
      Case Else 
       kala = 1 
       SavedThis = SaveSettings("preffered_theme", kala) 
      End Select 
       SetProjectionTheme 
     End If 

     'Add Key 
     If GetAsyncKeyState(vbKeyAdd) <> 0 Then 'code 107 
       SavedThis = SaveSettings("projection_font_size", SiteSettings("projection_font_size") + 2) 
       lblSongText.FontSize = SiteSettings("projection_font_size") 
     End If 

     'Subtract Key 
     If GetAsyncKeyState(vbKeySubtract) <> 0 Then 'code 109 
       SavedThis = SaveSettings("projection_font_size", SiteSettings("projection_font_size") - 2) 
       lblSongText.FontSize = SiteSettings("projection_font_size") 
     End If 

     'ESCAPE KEY 
     If GetAsyncKeyState(vbKeyEscape) <> 0 Then 
      Unload Me 
     End If 

    End Function 

    Private Sub trmListen_Timer() 
     Call getControls 
    End Sub 
+0

謝謝先生,這樣一個很好的答案,就像魅力一樣 –