0

I want to add a function to my buttonNewWithLabel, so it reacts to the enter key is pressed and just not only the onClicked event. I cant find how but should there not be as easy as the onClicked?

My code peice looks like this:

grt <- labelNew Nothing
str <- entryNew

but <- buttonNewWithLabel "Action"
but `onClicked` function1 str grt 
but `onEnterPushed`function1 str grt  <---  Something like this ?

Is there such a function in Gtk2Hs? Or...is there something else I can use?

shadow
  • 21,823
  • 4
  • 63
  • 77
user1501127
  • 865
  • 1
  • 18
  • 32
  • 2
    Buttons handle the enter key by default when they're focused. So, you'll likely want to read about how to do one of these two things with gtk (and then transfer that to gtk2hs): 1. focus a particular widget by default 2. add handlers to all the *other* widgets if you want an enter to click that button even when it's not focused. – Daniel Wagner Oct 19 '12 at 18:31
  • Thanks for the idea!, I will read up on focused widgets. //Regards – user1501127 Oct 20 '12 at 11:06

2 Answers2

1

I kept on looking by the side aswell as looking into widget focus as that is how it works.

An answer is to add a peice of code to the label that tells it to activate on entry.

What i got working is:

--Creating label & an entry
grt <- labelNew Nothing
str <- entryNew
str `onEntryActivate` function1 str grt   <-- This works like a charm!

--Create button
but <- buttonNewWithLabel "Action"
but `onClicked` function1 str grt 

The more i learn about Haskell & Gtk2Hs the more i like it! :)

//Regards

user1501127
  • 865
  • 1
  • 18
  • 32
1
import Graphics.UI.Gtk
import Control.Monad.IO.Class (liftIO)

main :: IO ()
main = do
  initGUI
  window <- windowNew
  button1 <- buttonNewWithLabel "button1"
  containerAdd window button1
  button1 `on` buttonPressEvent $ do
      liftIO $ putStrLn "button1 got clicked"   
      return True
  widgetShowAll window
  mainGUI
Chris
  • 56
  • 4