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?

2
  • 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. Oct 19, 2012 at 18:31
  • Thanks for the idea!, I will read up on focused widgets. //Regards Oct 20, 2012 at 11:06

2 Answers 2

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
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.